Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Nginx Spreadsheet Upload results in Bad Gateway
I have a Django application I'm working on and I want to upload a bunch of spreadsheets into it. However, I'm getting a bunch of 502 Bad Gateway errors inconsistently, which is blowing up my attempt to upload spreadsheets. Is there something I can do to my nginx config to help it deal? I've been uploading spreadsheets in batches of 500, which I'm willing to do, but even those have been failing, and I have 85,000 entries total to upload. These are only 100kb files so I'm not sure what the problem is. Here is my nginx config: server { server_name http://mydomain; # define buffers, necessary for proper communication to prevent 502s proxy_buffer_size 128k; proxy_buffers 4 256k; proxy_busy_buffers_size 256k; client_max_body_size 4g; access_log off; location /static/ { alias /webapps/myapp/static/; } location / { proxy_pass http://127.0.0.1:8001; proxy_set_header X-Forwarded-Host $server_name; proxy_set_header X-Real-IP $remote_addr; add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"'; } } If there are other configurations I should upload info for, please let me know. Thanks! -
django url pattern to match repeated get parameter
I'm writing a ListView that will show data for a selected list of models as identified by id. I want to the url pattern to be of the form: '/variables/?var=3&var=12&var=32' But I'm struggling to get the url regex to work. How should the regex be? -
Jquery clone of Django form - select elements fail validation
So I have an html page with a Django formset, and there are a certain number of forms initially, which varies with data. I'm using jQuery.clone() to clone the last form in the formset, and appending it. I'm incrementing the ids, etc, and then submitting the form after the user enters all the required data, which includes two select elements. All of the forms in the formset that were there when the page was loaded via GET validate fine. My problem is that any "cloned" forms in the formset are returning False during validation because the two select elements in the form are not returning a value, even though there is a value selected in the html page (again, I'm validating an option has been selected before allowing form submission). If I don't add any forms via the clone() method, everything works fine, and the submission works properly. I read the jquery docs and understand that "Note: For performance reasons, the dynamic state of certain form elements (e.g., user data typed into textarea and user selections made to a select) is not copied to the cloned elements", but I'm not relying on the clone to copy the values; this failure … -
Django Static Wont Load
I am learning how to use django but having difficulties using static files. In settings: STATIC_URL = '/static/' import os STATIC_ROOT = os.path.join(BASE_DIR, 'static') In URLS: from django.conf.urls import url, include from django.contrib import admin from django.conf import settings from django.conf.urls.static import static urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^', include('welcome.urls')), ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) In header.html: {% load static %} <link rel="stylesheet" type="type/css" href="{% static 'style.css' %}" /> I have ran collectstatic and it worked, but findstatic can't find style.css even though it is in the static directory. -
Django serializing model with custom many to many mapping
I have three models: class DistinctAlert(models.Model): entities = models.ManyToManyField(to='Entity', db_index=True, through='EntityToAlertMap') class Entity(models.Model): entity_instance = models.ManyToManyField(EntityInstance) class EntityToAlertMap(models.Model): entity = models.ForeignKey(Entity, on_delete=models.CASCADE) distinct_alert = models.ForeignKey(DistinctAlert, on_delete=models.CASCADE) entity_alert_relationship_label = models.ForeignKey(EntityAlertRelationshipLabel, on_delete=models.CASCADE, null=True) Disregarding the extra fields DistinctAlert and Entity has, this is what my serializer looks like: class EntitySerializer(serializers.ModelSerializer): entity_instance = EntityInstanceSerializer(many=True) class Meta: model = Entity fields = ('id', 'entity_instance') class EntityInstanceSerializer(serializers.ModelSerializer): entity_type = EntityTypeSerializer() class Meta: model = EntityInstance fields = ('label', 'entity_type') class DistinctAlertSerializer(serializers.ModelSerializer): entities = EntitySerializer(many=True) class Meta: model = DistinctAlert #TODO how do I serialize custom mapping? fields = ('id', 'entities') My problem is, with this, my api will return me just the entity, and it misses out the entity_alert_relationship field that's part of the EntityToAlertMap that I'm using to map entities to distinct alerts in a manytomany field. My question is, how can I serialize the DistinctAlert while maintaining the entity/relationship field -
Django Ajax not found url error: How to set url in function $.ajax({ ? [duplicate]
This question is an exact duplicate of: Django: strange not found url error Hello the problem is the next: In my app users can create goals, and all the functions works well. Now I am doing a notification function with ajax polling, and json. And It works fine when the user is new, if the user hasn't create a goal or modify anything, then the notification appears in every template of the app, (and I want that). But, if the user creates a goal, or modify others things in the app (others functionalities of the app that works well) then the notification works bad, because I get the error url not found. The stranger thing is that when the notification doesn't work the console shows: Not Found: /list_goal/notify Or If I am in some url of the app then, the error is Not Found: /some-url-of-the-app/notify But when It works, the console shows: "GET /notify/ HTTP/1.1" 200 125 So what can I do ? The code: goal/urls.py urlpatterns = [ url(r'^list_goals/$', views.list_goals, name='list_goals'), url(r'^list_goals/create/$', views.create, name='create'), url(r'^notify/$', views.notify, name='notify'), ... others urls ... ] goal/views.py @login_required def notify(request): user= User.objects.get(user=request.user.id) if request.method == 'GET': if request.is_ajax(): name = user.name data = … -
How to override a template from Django-Userena library?
So I am using django-userena and this library has it's own views, templates, etc. in /usr/local/bin/python2.7/dist-packages/userena I made my own templates and put them in my_project/templates/userena and these override whatever is in /usr/local/bin/python2.7/dist-packages/userena/templates (as long as it has the same name). However once I upgrade to Django 1.10 from 1.6, my templates are no longer overriding the supplied Userena ones. I am guessing it is due to the way Django handles templates after 1.6. views.py def signup(request, signup_form=SignupForm, template_name='userena/signup_form.html', success_url=None, extra_context=None): return ExtraContextTemplateView.as_view(template_name=template_name, extra_context=extra_context)(request) settings.py BASE_DIR = os.path.dirname(os.path.dirname(__file__)) PACKAGE_ROOT = os.path.abspath(os.path.dirname(__file__)) PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir)) TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [ os.path.join(BASE_DIR, 'templates'), ], 'APP_DIRS': True, 'OPTIONS': { 'debug': DEBUG, 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, }, ] Is there something I need to change in my settings? -
CSS style is missing in all my Django applications
Since I started using the Django framework I noticed that the CSS style did not appear when I entered the admin site and others templates and I thought it was normal but with the passage of time I have seen that the applications of my partners did have the style, so I copied their projects on my laptop and I ran them and I could see the CSS style of their applications. I'm getting data from a database from a server, that's the only difference in how my partners and I have been working. Does anyone know how I can fix this? I appreciate your answers, thank you :) PD: I'm working with version 1.8 of Django and version 3.4.4 of python -
Caching results of a Django function call with cache.get_or_set()
This clause works fine: if cache.get(cache_name): events = cache.get(cache_name) else: events = self.get_google_events() cache.set(cache_name, events, 60 * 10) If I put a print statement in get_google_events(), it's only printed if the cache is expired, as expected. But if I try to shorten the above to this: events = cache.get_or_set(cache_name, self.get_google_events(), 60 * 10) The print statement is fired every time (i.e. caching is silently broken). Isn't the second form equivalent to the first? -
Django user model, backward look up is not working
I've made a foreign key relationship with django User model, the forward lookup is working fine but when I try to backward is throwing this error: 'QuerySet' object has no attribute 'urlpost_set' I have also tried the related name! Also note that the Catagory to PostUrl and PostUrl to Catagory is working just fine! My models.py: from django.db import models from django.contrib.auth.models import User # Create your models here. class Catagory(models.Model): title = models.CharField(max_length=15, unique=True) created = models.DateField(auto_now_add=True) updated = models.DateField(auto_now=True) def __str__(self): return self.title class Meta: verbose_name_plural = 'catagory' class UrlPost(models.Model): STATUS_CHOICES = ( ('public', 'Public'), ('private', 'Private'), ) profile = models.ForeignKey(User, related_name='user_post', on_delete=models.CASCADE) catagory = models.ForeignKey(Catagory, on_delete=models.CASCADE) title = models.CharField(max_length=200) slug = models.SlugField(unique=True) url = models.URLField() status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='public') note = models.TextField(blank=True) created = models.DateField(auto_now_add=True) updated = models.DateField(auto_now=True) class Meta: ordering = ['-created'] verbose_name_plural = 'url Post' def __str__(self): return self.title -
Show Heroku database information in Django
Is there a way to display a database's information in a Django view? For example, I know in the Heroku cli you can do heroku pg:info -a your_app to view the database's table count, row count, type, etc. Is there a way to get this same information in Django to make is viewable in a template to keep track of storage, etc.? -
Django Storing A List of Logged In Users
I'm currently working on a login system in a Django app and I was wondering about how should I check if a user is logged in across all sessions. Logically speaking, if a user is logged in in, let's say, Browser #1, and is trying to log in from another place, Browser #2, should I just let them log in? Or should I also force log them out from the other place they were logged in (Browser #1)? Regardless the answer for my 1st question, what's the best way to store all logged in users or how to force log a user out from it's previous log in browser? Thanks in advance. -
Django: strange not found url error
the problem is the next: In my app users can create goals, and all the functions works well. Now I am doing a notify function with ajax polling, and json. And It works fine when the user is new, if the user hasn't create a goal or modify anything, then the notify appears in every template of the app, (and I want that). But, if the user creates a goal, or modify others things in the app (others functionalities of the app that works well) then the notify works bad, because I get the error url not found. The stranger thing is that when the notify doesn't work the console shows: Not Found: /list_goal/notify Or If I am in some url of the app then, the error is Not Found: /some-url-of-the-app/notify But when It works, the console shows: "GET /notify/ HTTP/1.1" 200 125 So what can I do ? The code: goal/urls.py urlpatterns = [ url(r'^list_goals/$', views.list_goals, name='list_goals'), url(r'^list_goals/create/$', views.create, name='create'), url(r'^notify/$', views.notify, name='notify'), ... others urls ... ] goal/views.py @login_required def notify(request): user= User.objects.get(user=request.user.id) if request.method == 'GET': if request.is_ajax(): name = user.name data = {"name":name } return JsonResponse(data) return render(request, 'list_goals.html') ... others views templates/base.html ... html code … -
Referntial objects gets deleted when deleting a foreign key in django model
I am making an audio player in django. When I delete the songs in a playlist, the song gets deleted even from the Song database. I would like to delete the song only from a playlist and not from the song database. But if a song gets deleted from song database, all its instances in playlists should also get deleted. models.py class Song(models.Model): song_title = models.CharField(max_length=250) file = models.FileField(upload_to='/',default = "null") class Meta: ordering = ['song_title'] def __str__(self): return self.song_title class Playlist(models.Model): name = models.CharField(max_length = 50) songs = models.ManyToManyField('Song', through='PlaylistTrack') def __str__(self): return self.name class PlaylistTrack(models.Model): playlist = models.ForeignKey(Playlist) track = models.ForeignKey(Song) views.py def deleteplaylistsong(request): if request.method == 'POST': song = (request.POST['song']) playlist = request.POST['playlist'] s = Song.objects.get(song_title=song) p = Playlist.objects.get(name=playlist) PlaylistTrack.objects.get(playlist = p,track = s).delete() return HttpResponse("success") -
Extracting zip file to Amazon s3 bucket in Django not working
Django setting.py has following DEFAULT_FILE_STORAGE = 'apps.storages.backends.s3boto.S3BotoStorage' MEDIA_ROOT = os.path.abspath(os.path.dirname(__file__)) + '/media/' I have a model like this class UploadFile(models.Model): def get_upload_path(self, filename): return os.path.join("documentsUploaded",filename) upload_location = models.FileField(upload_to=get_upload_path, max_length=300) text_location = models.FileField(upload_to=get_upload_path, max_length=300) This works fine and the file gets uploaded to s3 bucket as expected. So, now when i try to extract a zip file zf = zipfile.ZipFile(file_path) zf.extractall(os.path.join(MEDIA_ROOT,"documentsUploaded")) the files gets extracted to ... /meadia/ location in server but not to S3 bucket. What is that i am missing here? or is there a different way to extract .zip files onto s3 Django? -
NGINX not serving htm files, but serving all other static files
I've been banging away at this for a bit and I'm obviously missing something simple. I have a Django application I'm serving on nginx. All the other static files across the application are being served ok, except for some .htm files that are part of the library for the TinyMCE HTML editor. The path for the file not being served is: http://www.myurl.org/static/admin/tinymce/jscripts/tiny_mce/plugins/advlink/link.htm/ The nginx log file states the error is: "/home/deployer/cmp/cml/static/admin/tinymce/jscripts/tiny_mce/plugins/advlink/link.htm/index.html" is not found (20: Not a directory)" (Incidentally, I don't know why nginx keeps thinking that file path leads to a directory.) But, this test file: http://www.myurl.org/static/admin/tinymce/jscripts/tiny_mce/plugins/advlink/tst.html is served ok. My config file is: upstream app_server_wsgiapp { server localhost:8000 fail_timeout=0; } server { listen 80; server_name XX.XXX.X.XX; access_log /var/log/nginx/XX.XXX.X.XX.access.log; error_log /var/log/nginx/XX.XXX.X.XX.error.log info; keepalive_timeout 5; #nginx serve up static files and never send to the WSGI server location /static { include /etc/nginx/mime.types; autoindex on; alias /home/deployer/cmp/cml/static; } location / { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_redirect off; if (!-f $request_filename) { proxy_pass http://app_server_wsgiapp; break; } } #this section allows Nginx to reverse proxy for websockets location /socket.io { proxy_pass http://app_server_wsgiapp/socket.io; proxy_redirect off; proxy_buffering off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header … -
Django setting static file
I am learning django right now.The problem is setting the static file. I set the file according to the official document said.I first created a directory called 'static' in the app which my url link to ,and then made some changes on the template.The code is below: #The setting.py STATIC_URL = '/static/' #The template <!DOCTYPE html> {% load staticfiles %} <html> <head> <title>Main Site</title> <link rel='stylesheet prefetch' href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css'> <link rel= "{% static %}'css/style.css'" href="style.css"> Try a lot still don't get it work.Please help here. -
Django filter reversed foreignkey
I' ve these tables in a django app: class Order(models.Model): ... class SubOrder1(models.Model): order = models.ForeignKey(Order, blank=True, null=True) class SubOrder2(models.Model): order = models.ForeignKey(Order, blank=True, null=True) ... . How can i write a query on Order which results only the orders which has at least one related SubOrder1 or SubOrder2? So i need something like Order.objects.filter(suborder__count__ge = 1, ...) . Django = 1.9.2 Python = 3.4.1 . -
Django change admin - add categories to main screen
In the Django admin I want to take a few models that are all under the same app and arrange them by category. before is this: a regular admin and after the change I will add category "polls from users" (this isn't a new app) and will put "quesstion3" "question4" and "question5" under this category -
Can't create() nested relationship with DRF
This is the first time I'm working with DRF. My models: class ServiceCategory(models.Model): category = models.CharField(max_length=24) class Service(models.Model): service = models.CharField(max_length=24) category = models.ForeignKey('ServiceCategory') Their serializers: class ServiceCategorySerializer(serializers.ModelSerializer): class Meta: model = ServiceCategory fields = ('id', 'category') class ServiceSerializer(serializers.ModelSerializer): category = ServiceCategorySerializer() class Meta: model = Service fields = ('service', 'category') def create(self, data): return Service.objects.create(**data) And the view: elif request.method == 'POST': serializer = ServiceSerializer(data=request.data) print(serializer.initial_data) # To debug the contents of the request if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) Initially, before I added the nested category to the ServiceSerializer, I had no problem creating new Services. The print(serializer.initial_data) outputs <QueryDict: {'category': ['1'], 'service': ['EC2']}> so obviously I'm supplying the category to the request but I'm getting "category" : ["This field is required"] errors. So I'm thinking the problem might be with my create(self, data) method in the ServiceSerializer but I'm unable to put a finger on what exactly is wrong with it. What have I missed? -
Django doesn't see environment variables when deployed to Elastic Beanstalk
I'm trying to set up a Django/DRF application on Elastic Beanstalk, and for whatever reason, Django just can't see the desired environment variables. When I log in, I can see them just fine, by using $ eb ssh $ cat /opt/python/current/env I can also see them, except relatively sensitive ones involving RDS, simply using $eb printenv. All that appears to be set up and working properly. However, Django likes to read the environment immediately on starting up, and it appears that the environment variables just aren't set yet. I've experimented with simply inserting print(os.environ) in settings.py, and when I do that, I discover a whole bunch of environment variables which I don't need (i.e. 'SUPERVISOR_GROUP_NAME': 'httpd'), and none of the ones I've set myself, like DJ_SECRET_KEY. I've since changed the code to report the absence of specific environment variables when it loads the settings, and from a recent run, it generated this: [Wed Nov 23 15:56:38.164153 2016] [:error] [pid 15708] DJ_SECRET_KEY not in environment; falling back to hardcoded value. [Wed Nov 23 15:56:38.189717 2016] [:error] [pid 15708] RDS_DB_NAME not in environment; falling back to sqlite [Wed Nov 23 15:56:38.189751 2016] [:error] [pid 15708] AWS_STORAGE_BUCKET_NAME not in environment; falling back to … -
Django query count wrong
I have a model (called MyModel here) where I do MyModel.objects.filter(value__exact='').delete() to delete all objects with a blank value (value is a TextField with blank=True). However there seem to disappear more objects when I do this, than the count I get before deleting with MyModel.objects.filter(value__exact='').count() If I count blank valued MyOModel objects with a loop, using count = 0 for obj in MyModel.objects.iterator(): if not obj.value or obj.value.strip() == '': count += 1 I only get very few extra objects, compared tothe above count. What can cause this? I have checked related models that are referencing MyModel if they could cascade and somehow delete instances of MyModel without blank values. This is not the case. I'm on Django 1.8.7. -
Annotation summarizes results of two foreign models
I have the following models: from django.db import models APP_LABEL = 'qpair' class QPointSet(models.Model): class Meta: app_label = APP_LABEL db_table = 'qpointset' class QPoint(models.Model): x = models.FloatField(default=0.) pointset = models.ForeignKey(QPointSet, related_name='points') class Meta: app_label = APP_LABEL db_table = 'qpoint' class QPair(models.Model): first = models.ForeignKey(QPointSet, related_name='firsts') second = models.ForeignKey(QPointSet, related_name='seconds') class Meta: app_label = APP_LABEL db_table = 'qpair' And now I would like to count the number of points (QPoint) of the pointsets (QPointSet) assigned to the pair (QPair). Snippet to generate data: qps = QPointSet() qps.save() points = [QPoint(pointset=qps), QPoint(pointset=qps), QPoint(pointset=qps)] [v.save() for v in points] qps2 = QPointSet() qps2.save() points2 = [QPoint(pointset=qps2), QPoint(pointset=qps2), QPoint(pointset=qps2), QPoint(pointset=qps2)] [v.save() for v in points2] qpair=QPair(first=qps, second=qps2) qpair.save() Now I would like to know how many points are assigned as first and second pointset of my pair: # Testing that number of points are correct In [33]: QPointSet.objects.annotate(points_count=Count('points')).first().points_count Out[33]: 3 In [34]: QPointSet.objects.annotate(points_count=Count('points')).last().points_count Out[34]: 4 Great, that's what we expect. But I would like to have the information directly from the pair object! In [38]: QPair.objects.annotate(first_points_count=Count('first__points')).first().first_points_count Out[38]: 3 In [39]: QPair.objects.annotate(second_points_count=Count('second__points')).first().second_points_count Out[39]: 4 Perfect, that works. But can I have this in one db-call? In [42]: QPair.objects.annotate(first_points_count=Count('first__points'), second_points_count=Count('second__points')).first().first_points_count Out[42]: 12 What? 12 --> 3*4 … -
django-rest-auth reset password uid and token
Our team works on project with django-rest-api on backend and react on frontend. For authentication we use django-rest-auth, and we have problem with password reset. Here urls: urlpatterns += [ url(r'^accounts/', include('allauth.urls')), url(r'^admin/', include(admin.site.urls)), url(r'^api/', include('api.urls')), url(r'^rest-auth/', include('rest_auth.urls')), url(r'^rest-auth/registration/', include('rest_auth.registration.urls')), url( r'^rest-auth/password/reset/$', PasswordResetView.as_view(), name='password_reset', ), url(r'^rest-auth/password/reset/confirm/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$', PasswordResetConfirmView.as_view(), name='password_reset_confirm'), url( r'^rest-auth/registration/account-confirm-email/(?P<key>[-:\w]+)/$', confirm, name="account_confirm_email" ), url(r'^', include('core.urls', namespace='core')), ] When request to password_reset is posted, user receives email with link contains uid and token. Then user fills new_password in react form, and we want post data: { "new_password1": "new_password", "new_password2": "new_password", "uid": "", "token": "" } to /rest-auth/password/reset/confirm/. How we may to obtain this uid and token on frontend for make page with confirm password reset on this url, and then post data for confirm password change? -
Django- how to save form input to an object model in the database?
I have a Django project which is being used to manage a number of projects, and all of the information relating to a project. On one of the webpages, titled 'Concept', there is a form, which takes an input value of cost excluding vat in one text box, displays the cost including vat in another text box, and has a datepicker field to allow the user to select the date that a deposit has been paid, if it has at all. I'm having a bit of trouble getting the form to save the value entered by the user for the date at the moment- if the user fills out the three fields in the form (cost exc vat- entered by user, cost inc vat- automatically generated based on const exc vat, & date), and then refreshes the page, the two cost fields retain their values, but the 'date' field reverts to being blank. When I inspect the page element for the 'date' field, I can see that it is in the following HTML structure: <body> ... <div class="wrapper"> <div class="content"> <form method="POST" .... data-view-url=".../concept_save_ajax/"> <div class="col-12-box"> <div> <table class="right fixed m-t-md"> <tbody> <tr> ... <td class="p-r-md">Deposit exc VAT</td> <td class="p-r-md"> …