Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Can't get nginx to cache uwsgi result
I'm trying to cache a view in my uwsgi django app using nginx uwsgi caching. It mostly seems like nothing is getting written to the cache. The /data/nginx/cache/temp cache folder get's created but nothing gets written to it. I also applied the from django.views.decorators.cache import cache_page cache_page django decorator to the my_cached_page view so the result get's properly cached by django it self and the browser. But I want nginx to cache and return the result for everyone. I used uwsgi_ignore_headers to ignore the Set-Cookie header from the uwsgi app but doesn't seem to have any affect. I'm trying to better understand in which case a result get's cached ( or more importantly, not cached ). I think perhaps the django app is not returning the right headers for nginx to cache the result. Nginx version 1.11.2 http { include mime.types; log_format main '[$time_local] "$request" $status - $body_bytes_sent - $upstream_cache_status'; charset utf-8; client_max_body_size 300M; access_log /var/log/nginx/access.log main; uwsgi_cache_path /data/nginx/cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off; sendfile on; sendfile_max_chunk 512k; tcp_nopush on; tcp_nodelay on; gzip on; gzip_min_length 1000; gzip_comp_level 6; gzip_types text/plain text/css application/json application/javascript application/x-javascript text/javascript text/xml application/xml application/rss+xml application/atom+xml application/rdf+xml; gzip_buffers 16 8k; uwsgi_buffering on; uwsgi_buffers 8 16k; keepalive_timeout 65; … -
Django: max, min value with additional fields from selected row
I have a data set of products and those products have different price levels over time. (think an Amazon product) I'm trying to write a Django query to get the max and min prices for each product along with the date/time that it was that price. I can get the max and min with aggregate() but I can't include the date_seen field. Any ideas? models.py Class Product(models.Model): title = models.CharField(max_length=255) Class Price(models.Model): product = models.ForeignKey(Product, on_delete=models.CASCADE) date_seen = models.DateTimeField(auto_now_add=True) price = models.IntegerField(blank=True, null=True) -
psycopg2 import error when ubuntu upgraded to 17.10 (from 17.04)
Everything was working great until I upgraded the OS to Ubuntu 17.10. Now my Django project won't run (python manage.py runserver) because psycopg2 won't import. psycopg2 is already installed with pip (nothing has changed there). To be exact this is the error: lib/python3.5/site-packages/psycopg2/.libs/libresolv-2-c4c53def.5.so: symbol __res_maybe_init, version GLIBC_PRIVATE not defined in file libc.so.6 with link time reference -
How to use MultiPartParser in Django Rest Framework
I need to upload a file and some JSON associated with it. But I don't understand how to save the different parts, specifically the JSON part i.e. I'm able to save the image but not the JSON. I read that I need to use a MultiPartSerializer but I can't figure out how to integrate it correctly into my serializer class. Here is the multi-part request that my Server is receiving: <QueryDict: {'geo': ['{"point" : { "type:" : "Point", "coordinates" : [11.51350462236356, -22.70903491973877]}}'], 'picture': [<TemporaryUploadedFile: photo3.jpg (image/*)>]}> Here is the view: class UserUploadedPicture(APIView): def post(self, request, format=None): print(request.data) print("\n\n\n") serializer = PictureSerializer(data=request.data) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data) return JsonResponse(serializer.errors, status=400) Here is the serializer: class PictureSerializer(GeoFeatureModelSerializer): class Meta: model = Pictures geo_field = "point" fields = ('picture', 'processed', 'flagged', 'point') Here is the model: class Pictures(models.Model): objects = models.GeoManager() picture = models.ImageField(null=True, default=None, blank=True) processed = models.BooleanField(default=False) flagged = models.BooleanField(default=False) point = models.PointField(null=True, default=None, blank=True) Can anyone please tell me how to serialize the point field correctly? Maybe I need to change the JSON format? Maybe I need to change the serializer? -
Deploying django app with static content on AWS EB
I am trying to deploy a django app with static files to AWS EB following this guide https://realpython.com/blog/python/deploying-a-django-app-to-aws-elastic-beanstalk/ I have the standard folder structure with project and app set up. I am using sqlite so no database set up on AWS is required. My python.config file looks like this container_commands: 01_collectstatic: command: "source /opt/python/run/venv/bin/activate && python /opt/python/current/app/manage.py collectstatic --noinput" option_settings: "aws:elasticbeanstalk:application:environment": DJANGO_SETTINGS_MODULE: "myappproject.settings" "PYTHONPATH": "/opt/python/current/app/myappproject:$PYTHONPATH" "ALLOWED_HOSTS": "myappawseb.eu-central-1.elasticbeanstalk.com/" "aws:elasticbeanstalk:container:python": WSGIPath: "myappproject/wsgi.py" NumProcesses: 3 NumThreads: 20 "aws:elasticbeanstalk:container:python:staticfiles": "/static/": "www/static/" I previously did pip freeze and have my requirements.txt file in the project folder. After eb deploy I get an import error for my views.py on myapp.eu-central-1.elasticbeanstalk.com/: ImportError at / cannot import name 'views' Request Method: GET Django Version: 1.11.3 Exception Type: Traceback: ImportError Exception Value: cannot import name 'views' Exception Location: /opt/python/current/app/myapp/views.py in , line 5 Python Executable: /opt/python/run/venv/bin/python3 Python Version: 3.4.3 Python Path: ['/opt/python/run/venv/lib64/python3.4/site-packages', '/opt/python/run/venv/lib/python3.4/site-packages', '/opt/python/current/app/myapp, '/opt/python/current/app', '', '/opt/python/run/baselinenv/local/lib64/python3.4/site-packages', '/opt/python/run/baselinenv/local/lib/python3.4/site-packages', '/opt/python/run/baselinenv/lib64/python3.4', '/opt/python/run/baselinenv/lib/python3.4', '/opt/python/run/baselinenv/lib64/python3.4/site-packages', '/opt/python/run/baselinenv/lib/python3.4/site-packages', '/opt/python/run/baselinenv/lib64/python3.4/plat-linux', '/opt/python/run/baselinenv/lib64/python3.4/lib-dynload', '/usr/lib64/python3.4', '/usr/lib/python3.4'] My eb error log looks like this: File "<frozen importlib._bootstrap>", line 2237, in _find_and_load [Thu Oct 26 23:41:07.082933 2017] [:error] [pid 10378] File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked [Thu Oct 26 23:41:07.082935 2017] [:error] [pid 10378] File "<frozen importlib._bootstrap>", line 1200, in … -
Understanding cookies, sessions, and subdomain routing in django
I've read a lot of answers here but none of them really go in depth of what's happening and I don't have a good understanding due to this. If I have a django app hosted on heroku. Let's say the appname is "name" so it's available and being served at name.herokuapp.com. Now, let's say I have multiple domains 1.com1, 2.com, etc which all point to this app. My login on the main domain 1 won't be shared across 2 etc. Similarly, session data such as a session based shopping cart wouldn't be either. But, let's say I changed these to subdomains and set the SESSION_COOKIE_DOMAIN to be .1.com and had the other domains 2 and beyond go to subdomains. So, now 2.1.com etcetera. Would the login be shared since the cookie is set at the top level domaina nd similarly the session data such as the shopping cart? Now, if I can't change these to subdomains how would I solve the above issues of login and sharing the cart between the domains pointed to my app? -
Is it safe to use @csrf_exempt to send data from a Wordpress (or other external) site if there is no user authentication?
I’m making a simple Django app that takes data from a user submitted form, creates an image based on the data and displays the image to the user. The problem is that the form needs to be on an existing Wordpress site. The app will only perform this one task, and the image will be discarded, so there will be no user authentication, sessions, or anything like that. Is it safe to use @csrf_exempt to make this work? Some of the answers to Do CSRF attacks apply to API's? suggest that a csrf token is not necessary if there is no user auth. However, some of the answers also suggest that there should be some other form of authentication in place instead. And the answer to Django - CSRF verification failed - send data from form on static website to my django app suggests that there is a way to add a csrf token to a third party site, and this would make @csrf_exempt unnecessary. Which of these approaches is right? If my understanding of csrf is correct it makes sense to me that there is no risk of csrf without user authentication. Such an attack would not be able … -
How do you prefetch django user groups for permissions checks
I have a class that has permissions are checked over and over. Each time the request user's groups are re-queried from the database instead of caching them making thing really slow. How can I prefetch or cache the groups in the request user? class Object(models.Model): ... def has_object_read_permission(self, request): if ('test' not in request.user.groups.all()): return False return True -
Extendings Django's UserCreationForm
I'm trying to extend Django's django.contrib.auth.forms.UserCreationForm to include email and name fields. My similar extension of the UserChangeForm works fine, but UserCreationForm still only shows the default username, password1, and password2 fields. Any ideas what I'm doing wrong? forms.py class AuthorCreation(UserCreationForm): class Meta: model = User fields = ('username', 'password1', 'password2', 'first_name', 'last_name', 'email', 'groups', 'is_staff') class AuthorChange(UserChangeForm): class Meta: model = User fields = ('username', 'first_name', 'last_name', 'email', 'password', 'groups', 'is_staff') admin.py class AuthorAdmin(UserAdmin): """Admin class for creating and managing Authors""" inlines = [AuthorInline] fieldsets = ( (None, { 'fields': ('username', ('first_name', 'last_name'), 'email', 'password', 'groups', 'is_staff') }), ) class Media: css = { 'all': [ 'css/codenotes-admin.css', ] } def get_form(self, request, obj=None, **kwargs): if obj is None: return forms.AuthorCreation else: return forms.AuthorChange admin.site.unregister(User) admin.site.register(User, AuthorAdmin) Again, the AuthorChangeForm correctly displays all the fields, but AuthorCreationForm only displays the username, password1, and password2 fields (plus the inline forms, which works fine on both). I assume the problem is with the fieldsets, but I can't figure it out from the docs. -
Django : pagination for two different models
I am working on a Django (v1.11.0) project and I have two models called Song that contain users songs and GenericSong that contain administration songs. class Song(models.Model): album = models.ForeignKey(Album, on_delete=models.CASCADE) song_title = models.CharField(max_length=250) audio_file = models.FileField() is_favorite = models.BooleanField(default=False) def __str__(self): return self.song_title class GenericSong(models.Model): album = models.ForeignKey(GenericAlbum, on_delete=models.CASCADE) song_title = models.CharField(max_length=250) audio_file = models.FileField(default='') is_favorite = models.BooleanField(default=False) def __str__(self): return self.song_title I want to list all the songs in the database which contain users and administration songs, the problem is, I want to list that with a pagination. I did this for a page that contain only users songs using this view, which is working just fine : @login_required() def songs(request, filter_by=None): users_songs = Song.objects.filter(album__user=request.user) page = request.GET.get('page', 1) if filter_by == 'favorites': users_songs = users_songs.filter(is_favorite=True) paginator = Paginator(users_songs, 10) try: songs_page = paginator.page(page) except PageNotAnInteger: songs_page = paginator.page(1) except EmptyPage: songs_page = paginator.page(paginator.num_pages) return render(request, 'music/songs.html', { 'song_list': songs_page, 'filter_by': filter_by, }) But in the second view, I don't know how to make song and genericsong in one variable to make tha pagination, here is the all songs view that I want to make changes to it : @login_required() def all_songs(request, filter_by=None): users_songs = Song.objects.all() generic_songs = … -
Django accese related model
theres's something I'm missing or don't understand, maybe a very basic concept. I have two models Person and Student, related by a OneToOneField like this: class Student(models.Model): person = models.OneToOneField( Person, on_delete = models.CASCADE, ) # fields definition Should't I be able to access Student from Person with student_set? Like this, but I get de error as follows: >>> from people.models import Person >>> p = Person.objects.get(pk=6) >>> p.student_set.all() Traceback (most recent call last): File "<console>", line 1, in <module> AttributeError: 'Person' object has no attribute 'student_set' >>> Thanks! -
Daemon process alongside django
Like to see what others think the best architecture for the following. I am trying to build a project that with a web front end that has the requirement to issue commands to other long running processes that will return results to the django db. The app must be scalable (in both db and processing power) and be able to run quickly. I've thought of the following scenario but wanted to check if there is a better way. Django web app receives instructions from a user (via web form or API) puts this 'request' into a queue (which technology would be best?) that another process (running asynchronously to gain performance, no startup costs) can consume (how does it do this? By monitoring a queue? IPC?). This app then returns the result of it's processing which needs to be put back into the django db (best to do this by another queue or direct access?) Thoughts on sanity...and technology. Links to tutorials would be great! How to run the asynchronous process. Celery, host daemon process? How to get data to and from the main django app/db. Queues, socket IPC? Use of django signals in here somewhere?! -
Why my django program is not working?
I am a beginner of Django . I started learning django very early . But now my program is not running ? what is wrong with it? -
share login across domain without cas server
How would I share django login authentication across domains? The app is hosted on heroku and the other domains simply point to the app and the urls are rerouted according to the domain. I tried setting cookies per domain but this didn't work. CAS server isn't an option. What are other options? -
requests.post hanging for a minute locally
I am attempting to post to recaptcha via Python requests module. I have had no problems until recently there is a one minute delay for the post request to process, only locally. In Heroku production there is no delay. Possible reasons why this issue might have just now cropped up locally? I have 127.0.0.1 as a valid domain for the recaptcha. accesscode = request.POST.get('g-recaptcha-response') url = 'https://www.google.com/recaptcha/api/siteverify' secret = settings.RECAPTCHA_PRIVATE_KEY postdata = { 'response': accesscode, 'secret': secret, } r = requests.post(url, data=postdata) -
Django Rest Framework Validate Child in ListField
I have created a list of DictField instances. Here is my serializer.py class DetailSerializer(serializers.Serializer): """Serialize order""" id = serializers.IntegerField(read_only=True) order= serializers.IntegerField(min_value=1) class OrderSerializer(serializers.ModelSerializer): orderlist = DetailSerializer(many=True) Now I want to do validation for orderlist like make sure every order in this list is strictly_increasing and unique. How can I do? -
How do i bind a react component to the database state?
I want to bind the state of a react component to a backend server running on Django. What i am trying to achieve is an ajax call that constantly updates the state of the component, however, constantly making an api call after every couple of seconds might have an impact on the performance of the application and hence, i am looking for a better way to achieve the same. -
How can I authenticate user both in websockets and REST using Django and Angular 4?
I would like to authenticate user both in websockets and REST using Django and Angular 4. I have created registration based on REST API. User after creating an account and log in can send messages to backend using websockets. My question is how can I know that the user authenticated by REST API (I use Tokens) is the same user who sends messages? I don't think that sending Token in every websocket message would be a good solution. Do you have any ideas? -
I need a blog app for my Django project
I'm going to add a "/blog/" to my django project and somehow I do not want to write it from scratch. So i'm looking for a blog app (like WordPress) and even have found some but they are either so simple or they're not an APP (like Mezzanine). So what do you suggest? -
How to create foreign key related objects during object create call with the Django Rest Framework?
Let's say I have model A and B. Model A has a range of fields including a user field which is filled in with the user who creates an object A. When object A is created I want to create a bunch of object B's using the user that created object A and possible using object A itself as a foreign key. My Model A ViewSet looks like this: class OrganizationViewSet(DynamicModelViewSet): queryset = Organization.objects.all() serializer_class = OrganizationSerializer permission_classes = (IsAdminOfPermission,) So when an organization is created I want to create a bunch of object B's using the user that created the organization and possibly using the organization object itself. How do I do this? -
How to extract a list of points from Django PolygonField?
In one of my models that contains a PolygonField I have a to_dict method that takes all the values and turns them into a readable dictionary. I do a similar thing for a model that has a PointField and it looks like this: 'point': { 'type': 'Point', 'coordinates': [self.point.x, self.point.y] }, For the PolygonField I have to loop through the points to put them in the dictionary. I tried this but as expected, django complained: 'polygon': { 'type': 'Polygon', 'coordinates': [ for point in self.path: [point.x, point.y] ] }, How do you add all the points from a PolygonField into a dictionary? -
Django updating dropdown list from selection
I'm using class-based views to populate a form. My model has two many-to-one foreign keys. I want to populate one of the dropdowns depending on the other dropdown which is based on the foreign keys. models.py class Assignment(models.Model): is_section = models.BooleanField(default=False) assignment_self = models.ForeignKey('self', null=True, blank=True, limit_choices_to={'is_section': True}, verbose_name='Assignment', on_delete=models.CASCADE) assignment_course = models.ForeignKey('Course', default=None, null=True, blank=True, on_delete=models.CASCADE) In views.py, the 'assignment_self' is the queryset I want to update when the dropdown for assignment_course is updated. class AssignmentCreate(CourseMixin, CreateView): model = Assignment fields = ['is_section', 'assignment_self', 'assignment_course'] def get_initial(self): pk = self.kwargs['pk'] course = Course.objects.get(pk=pk) return {'assignment_course': course} def get_form(self, form_class=None): pk = self.kwargs['pk'] course = Course.objects.get(pk=pk) form = super(AssignmentCreate, self).get_form(form_class) form.fields['assignment_self'].queryset = Assignment.objects.filter(assignment_course=course) return form assignment_form.js $('#id_assignment_course').change(function(){ $('#section-modal-label').text("Add to " + $('#id_assignment_course option:selected').text()); $.ajax({ type: 'GET', data: { course_selected: $('#id_assignment_course option:selected').text(), }, success: function(result){ alert('ok'); }, error: function(result){ alert('error'); } }) }) assignment_form.hml {{ form.as_p }} urls.py url(r'^add_assignment/(?P<is_section>\d+)/(?P<pk>\d+)/$', AssignmentCreate.as_view(), name='add_assignment'), How do I get the course_selected from the assignment_form.js to the views.py? -
Log http requests in db table with django and tastypie without increase response time
I have android app on which my django server is sending some push notifications using firebase cloud messaging. As soon as client received the notifications, it makes a GET request to my API written using Tastypie. I want to somehow store this GET request in my Log table so that I can ensure that my android app is working fine and make right requests on receiving notifications from server. On one of the answers, I read that it can be achieved by overriding the dispatch function for Model Resource in tastypie. Could this logging be achieved without overriding the dispatch because it can potentially increase the response time. -
Querying / Retrieving Data from SQL Server using Django
I am pretty new to Django. I am trying to query data from SQL server using a Django App. I am able to connect with the default Django database and fetch data from there but Django keeps throwing an error when I try to query an existing database. (Apologies for indentation mistakes in formatting here but it is ok in my scripts) views.py shows the following error: from django.http import HttpResponse from django.template import Context,Template,RequestContext from django.shortcuts import render_to_response, render from django.conf.urls.static import static from django.contrib import admin from django.contrib.staticfiles.urls import staticfiles_urlpatterns from django.template.context_processors import csrf from ForecastApp.forms import * from django.core.mail import send_mail from django.utils.safestring import mark_safe #from django.db import connection import os import csv import json import subprocess from subprocess import * from operator import itemgetter from datetime import datetime import pyodbc from logging import getLogger from .models import Actuals from .models import forecastfuture ss = forecastfuture.objects.using('default').all() print len(ss) for row in ss: print (row.region) #works fine tt = Actuals.objects.using('Forecasting').all() print len(tt) for row in tt: print (row.Region) [Microsoft][SQL Server Native Client 11.0][SQL Server]Invalid column name 'id'. (207) I have set up routers.py and models.py and settings.py per instructions in documentation. routers.py class App1Router(object): #pardon my poor indentations … -
Django-shared-session is not working properly despite instructions
Does anyone have an example of using django-shared-session successfully? Per the instructions I've set the domains included the shared session tempalte tags etcetera. However, the login between domains isn't being shared despite the package saying it will share logins and session data.