Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
How to convert to and from timestamp in Python without loosing microsecond precision?
What on earth is going on??? I figure it must have something to do with float imprecision, but how can I fix it? > from datetime import datetime > from django.utils.dateparse import parse_datetime > for i in range(10): ... dt = parse_datetime('2016-09-13T14:32:10.40455{}'.format(i)) ... ts = dt.timestamp() ... dt2 = datetime.fromtimestamp(ts) ... print("{} == {}: {}".format(dt, dt2, dt == dt2)) 2016-09-13 14:32:10.404550 == 2016-09-13 14:32:10.404550: True 2016-09-13 14:32:10.404551 == 2016-09-13 14:32:10.404551: True 2016-09-13 14:32:10.404552 == 2016-09-13 14:32:10.404551: False 2016-09-13 14:32:10.404553 == 2016-09-13 14:32:10.404552: False 2016-09-13 14:32:10.404554 == 2016-09-13 14:32:10.404553: False 2016-09-13 14:32:10.404555 == 2016-09-13 14:32:10.404555: True 2016-09-13 14:32:10.404556 == 2016-09-13 14:32:10.404556: True 2016-09-13 14:32:10.404557 == 2016-09-13 14:32:10.404556: False 2016-09-13 14:32:10.404558 == 2016-09-13 14:32:10.404557: False 2016-09-13 14:32:10.404559 == 2016-09-13 14:32:10.404558: False PS: I'm on Python 3, Django 1.9.6 -
How to generate a 100% guaranteed unique id for filenames?
In my app, I have a separate server for serving files and uploading them, and in django I have a generic "Media" model which will store an unique id so that I have a reference to the files. How should I generate a unique alphanumeric id that I can use as filenames? And how do I make sure that there will be no collisions? I want to avoid using something of big length like uuid. An example media/avatars_as24Gvs4211v_t200x200.png -
Django Capturing multiple url parameters in request.GET
i am trying to get all query parameters out of an request url/?animal__in=dog,cat&countries__in=france I tried animals = request.GET.get('animal__in','') countries = request.GET.get('countries__in','') but then animals and countries are not lists, they are just strings. Is there a more django way to do this whole capturing? -
Django's X-Frame-Options is working on local machine but not elsewhere
Our web application has a few views that need to be embedded in an iframe while the rest (a lot of them) shouldn't. We are using Django 1.9.7 and have the following added to the settings: MIDDLEWARE_CLASSES = ( ... 'django.middleware.csrf.CsrfViewMiddleware', 'cassandra_auth.middleware.AuthenticationMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ... ) The views that need to be displayed in the iframe are then marked as @xframe_options_exempt on them: @csrf_exempt @xframe_options_exempt def get_user_transactions(request, custom_user_id): user = CustomUser.objects.get(id=custom_user_id) context = {"customer": user, "customer_id": custom_user_id} html = render(request, 'help/faq.html', context) return HttpResponse(html) When i run this on my local machine (localhost), I can find that the X-Frame-Options header is suppressed in the HTTP response for this view. However, when I move my code to an internal cloud (our dev and test boxes), the X-Frame-Options header is delivered with a value SAMEORIGIN. Any help is greatly appreciated. Thanks! -
Django - trouble linking to new posts
I have searched far and wide, picking up tidbits of code along the way but the trouble is different tidbits may not be translating into a cohesive representation of what I want. Basically I have a page where I can create new blog posts at ".../progresstracker/new" which, when I click submit I want the new post to be available at ".../progresstracker//" and I have a landing page at ".../progresstracker" which should have summaries of all the posts and links to the appropriate // URL. I think the issue is with the pk/slug but there could be several errors realistically. url: urlpatterns = [ url(r'^progresstracker/(?P<category>[\w\s]+),(?P<slug>[\w\s]+)/$', views.pt_detail, name='pt_detail'), url(r'^progresstracker/new/$', views.progresstracker_new, name='progresstracker_new'), url(r'^progresstracker/(?P<category>[\w\s]+),(?P<slug>[\w\s]+)/edit/$', views.post_edit, name='post_edit'), url(r'^progresstracker/', views.progresstracker, name='progresstracker'), ] views: def pt_detail(request, slug): ptpost = get_object_or_404(Post, slug=slug) return render(request, 'blog/pt_detail.html', {'ptpost': ptpost}) def progresstracker(request): posts = Post.objects.order_by('published_date') return render(request, 'blog/progresstracker.html', {'posts': posts}) def progresstracker_new(request): if request.method == "POST": form = PostForm(request.POST) if form.is_valid(): ptpost = form.save(commit=False) ptpost.author = request.user ptpost.published_date = timezone.now() ptpost.save() return redirect('pt_detail', slug=ptpost.slug) else: form = PostForm() return render(request, 'blog/post_edit.html', {'form': form}) def post_edit(request, slug): ptpost = get_object_or_404(Post, slug=slug) if request.method == "POST": form = PostForm(request.POST, instance=ptpost) if form.is_valid(): ptpost = form.save(commit=False) ptpost.author = request.user ptpost.published_date = timezone.now() ptpost.save() return … -
Response of POST request timeout from gunicorn + nginx server
I am sending a post request from a method inside a web application running on django+nginx+gunicorn. I have no issues receiving 200 response from the same code when executed on django's own server (using runserver). try: response = requests.post(post_url, data=some_data) if response.status_code == OK and response.content == '': logger.info("Request successful") else: logger.info("Request failed with response({}): {}".format(response.status_code, response.content)) return response.status_code == OK and response.content == '' except requests.RequestException as e: logger.info("Request failed with exception: {}".format(e.message)) return False I checked the server logs at post_url, it is indeed returning 200 response with this data. However, when I run the app behind gunicorn and nginx, I am not able to receive the response. The code gets stuck, gunicorn worker times out (after 30 seconds). This is the apache server log at the post_url: [14/Sep/2016:13:19:20 +0000] "POST POST_URL_PATH HTTP/1.0" 200 295 "-" "python-requests/2.9.1" -
Comments via Contenttype: what about reference integrity
Django 1.10 I would like to organize commenting. The problem with thie code below is reference integrity. I mean that when the object that was commented upon is deleted, the comment stays in the database. I would like the behaviour similar to on_delete=models.PROTECT. But if it is too hard, equivalent of CASCADE will be also Ok. By the way, if Contenttype is not appropriate here, please let me know. But I want the comments to be applicable to several models. Could you help me understand what is the best way to cope with this problem. class Comment(models.Model): author = models.ForeignKey(User, on_delete=models.PROTECT) body = models.TextField(blank=False, null=False, default="", verbose_name = "Текст",) content_type = models.ForeignKey(ContentType) object_id = models.PositiveIntegerField() content_object = GenericForeignKey('content_type', 'object_id') -
Naturaltime with minutes, hours, days and weeks
I use naturaltime in my Django application. How can I display only time in minutes, then hours, then days, then weeks? Here is my code: {{ obj.pub_date|naturaltime }} -
Django: app level object
I've created a Django-rest-framework app. It exposes some API which does some get/set operations in the MySQL DB. I have a requirement of making an HTTP request to another server and piggyback this response along with the usual response. I'm trying to use a self-made HTTP connection pool to make HTTP requests instead of making new connections on each request. What is the most appropriate place to keep this app level HTTP connection pool object? I've looked around for it & there are multiple solutions each with some cons. Here are some: To make a singleton class of the pool in a diff file, but this is not a good pythonic way to do things. There are various discussions over why not to use singleton design pattern. Also, I don't know how intelligent it would be to pool a pooler? (:P) To keep it in init.py of the app dir. The issue with that are as follows: It should only contain imports & things related to that. It will be difficult to unit test the code because the import would happen before mocking and it would actually try to hit the API. To use sessions, but I guess that makes … -
Django submit form using Request Module, CSRF Verification failed
I am trying to submit a form of django application using Python request module, however it gives me the following error Error Code: 403 Message: CSRF verification failed. Request aborted. I tried to convert the in JSON using json.dumps and sent the request, however I am getting the same error. I am not sure, what is missing. When I submit the form using UI, it works well. I intercepted the request using Postman plugin as well and the request I have form is same. Following is the code I am using. import requests import json r = requests.get("http://localhost:8000/autoflex/addresult/") csrf_token = r.headers.get("Set-Cookie").split(";")[0].split("csrftoken=")[1] cookie = "csrftoken=%s" % csrf_token headers = {"Content-Type": "Application/x-www-form-urlencoded", "Cookie" : cookie, "Accept-Encoding" :"gzip, deflate", "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"} data = {"xml_url": xml_url, "csrfmiddlewaretoken": csrf_token} result = requests.post("http://localhost:8000/autoflex/addresult/", data=data, headers=headers) print result.request.body print(result.status_code, result.reason, result.content) -
Display ManyToManyField as list in FormView
I would like to be able to display a ManyToManyField as two elements when presenting a formview. One element should contain a queryset and the second element should be a receiver for a prioritized list based on the queryset. Here's a small example: Say I have a website that registers potential new zoos and which animals they would like to have in a prioritized list. Note that not all zoos want every animal from the database. I write the following django code to accommodate their needs: models.py class Zoo(models.Model): name = models.CharField(max_length=50) Desired_animals = models.ManyToManyField('Animal', through='ZooMeta') class Animal(models.Model): Species = models.CharField(max_length=50) Dangerous = models.BooleanField() def __str__(self): return str(self.Species) class ZooMeta(models.Model): Zoo = models.ForeignKey(Zoo) Animal = models.ForeignKey(Animal) Priority_rank = models.PositiveIntegerField() forms.py class ZooForm(forms.ModelForm): class Meta: model = Zoo fields = {'name', 'Desired_animals'} views.py class ZooFormView(FormView): form_class = ZooForm template_name = "NewZoo/zoo_form.html" succes_url = '/succes/' zoo_form.html <form role="form" action="" method="post"> {% csrf_token %} {{ form.as_p }} <input type="submit" value="Submit" /> </form> I would like to be able to do something like: Ideal HTML <form role="form" action="" method="post" id=jquery_function_identifier> {% csrf_token %} <ul id=query_list> {% for animal in form.animals %} <li>{{ animal }}</li> {% endfor %} </ul> <ul id=prioritised_list> <li>placeholder</li> </ul> <input type="submit" … -
Django REST WSGI client denied by server configuration
I have RESTful API running with Django/WSGI server running but I can not access it from my web application. I can successfully query it with httpie from a terminal. Start server python manage.py runmodwsgi --host 0.0.0.0 --port 8001 --https-port 8000 --ssl-certificate-file ../utils/ssl_cert/local.crt --ssl-certificate-key-file ../utils/ssl_cert/local.key --processes 8 --server-name localhost --https-only --reload-on-changes --include-file ./extra.conf http.conf generated by wsgi: https://gist.github.com/NicolasRannou/1b6c2c22b3b8e5179d16683ab026238a extra.conf (to allow CORS): Header always set Access-Control-Allow-Origin "*" If I try to query my REST API from the web app (https://127.0.0.1:8001/api/v1/auth-token), I get the following warning in the wsgi error log file: [Wed Sep 14 08:16:03.702537 2016] [authz_core:error] [pid 5837:tid 140271486973696] [client 10.0.2.2:52392] AH01630: client denied by server configuration: /tmp/mod_wsgi-0.0.0.0:8001:1000/htdocs/api and the following JS error: XMLHttpRequest cannot load https://127.0.0.1:8001/api/v1/auth-token/. Response for preflight has invalid HTTP status code 403 What seems to happen is that the server tries to access a directory api/v1/auth-token/ that doesn't exist as the url is only used for the Restful API and doesn't reflect the file system tree. Would it be a mod_wsgi or a Django misconfiguration? Is is possible to by-pass the warning with extra apache configuration in the extra.conf file? Best, -
How to make any method from view/model as celery task
I have some of the analytics methods in models.py under class Analytics (e.g: Analytics.record_read_analytics()). And we are calling those methods for recording analytics, which doesn't need to be synchronous. Currently it's affecting rendering of each request so decided to add these methods in celery queue. We are already using celery for some of our tasks hence we have tasks.py and celery.py file. Following is section of models.py file: class Analytics(): ... ... @staticmethod def method_a(): ... ... def method_b(): ... ... @staticmethod def record_read_analytics(): ... ... I don't wanted to write again same model level class methods in tasks.py and wanted to make some of view method's and model level class methods as celery task. Following is celery.py file: from __future__ import absolute_import from celery import Celery app = Celery('gnowsys_ndf', include=['gnowsys_ndf.tasks']) app.config_from_object('gnowsys_ndf.celeryconfig') if __name__ == '__main__': app.start() I'm new to celery and looking for help. Thank you in advance. -
Extending Djagno 1.10 admin template for site integration / custom navigation?
I'm tying to find a way to extend Django 1.10 template to include a custom menu or site integration. What is the best method for this? -
Access m2m relationships on the save method of a newly created instance
I'd like to send emails (only) when Order instances are created. In the email template, I need to access the m2m relationships. Unfortunatly, its seems like the m2m relations are ont yet populated, and the itemmembership_set.all() method returns an empty list. Here is my code: class Item(models.Model): ... class Order(models.Model): ... items = models.ManyToManyField(Item, through='ItemMembership') def save(self, *args, **kwargs): pk = self.pk super(Order, self).save(*args, **kwargs) # If the instance is beeing created, sends an email with the order # details. if not pk: self.send_details_email() def send_details_email(self): assert len(self.itemmembership_set.all()) != 0 class ItemMembership(models.Model): order = models.ForeignKey(Order) item = models.ForeignKey(Item) quantity = models.PositiveSmallIntegerField(default=1) -
How to save HTML array inputs
I have a form like this: <input name="field[1][title]" type="text"> <input name="field[1][placeholder]" type="text"> <input name="field[1][default]" type="text"> <input name="field[2][title]" type="text"> <input name="field[2][placeholder]" type="text"> <input name="field[2][default]" type="text"> <input name="field[2][max]" value="100" type="number"> <input name="field[3][title]" type="text"> <input name="field[3][placeholder]" type="text"> <input name="field[3][default]" type="text"> <input name="field[4][title]" type="text"> <input name="field[4][placeholder]" type="text"> <input name="field[4][default]" type="text"> <input name="field[4][max]" value="100" type="number"> ... How I can save each field as a model row in django? Something like this: for x in len(field): row = Row() row.title = field[x][title] row.placeholder = field[x][placeholder] row.default = field[x][default] row.save() -
elasticsearch-dsl wrong sorts
Help me, please, resolve the problem. I use elasticsearch-dsl and i'm getting chunks of data about photos, where every chunk sorted by date_to and other filters, but chunks are following each other and don't save sort order, like here: 1st chunk: 2000-01-01, 2000-01-20, 2001-01-01, 2002-10-30 2nd chunk: 2001-02-01, 2002-01-01, 2003-01-01, 2003-01-02 3rd chunk: 2003-01-01, 2003-01-02, 2003-04-01, 2003-12-31 My doctypes.py from elasticsearch_dsl import DocType, String, Boolean, Integer, GeoPoint, Date class PhotoDocType(DocType): id = Integer() name = String(analyzer='my_analyzer') description = String(analyzer='my_analyzer') year_from = Integer() year_to = Integer() date_from = Date() date_to = Date() class Meta: index = settings.ES_INDEX doc_type = 'photos_photo' @classmethod def from_obj(cls, obj): data = { 'id': obj.id, 'name': obj.name, 'description': obj.description, 'date_from': obj.date_from, 'date_to': obj.date_to, } if obj.date_from: data['year_from'] = obj.date_from.year if obj.date_to: data['year_to'] = obj.date_to.year return cls(**data) My model.py import datetime import calendar from pytils.dt import ru_strftime def photo_display_dates(date_from, date_to): if not (date_from and date_to): return '' if date_from == date_to: return ru_strftime('%d %B %Y', date_from, inflected=True) is_first_day_of_year = (date_from.day, date_from.month) == (1, 1) is_last_day_of_year = (date_to.day, date_to.month) == (31, 12) is_same_year = date_from.year == date_to.year if is_first_day_of_year and is_last_day_of_year: if is_same_year: return '{} - {} {} г.'.format(ru_strftime('%B', date_from), ru_strftime('%B', date_to), date_from.year) if date_to.year - date_from.year == 9 … -
Getting an ""auth_user" does not exist" error in django 1.9 after recreating the postgres database
I dropped and recreated the database for my Django project recently. I also removed all the migration folders for each of the apps. When running the migrations, I get an error saying django.db.utils.ProgrammingError: relation "auth_user" does not exist LINE 1: ...user"."is_active", "auth_user"."date_joined" FROM "auth_user... I tried researching this error, and most advice seems to be to run the migrations of the auth folder first: python manage.py migrate auth but I still get the same error when doing this. Any ideas what I might be doing wrong? -
Wrong data display on table using Django Tables2
I'm trying to display a table with data but when rendering the table I get a wrong data. I have 2 tables, each one on a diferent template. The first table it's on menu/costHistory.html, the url it's url(r'^costHistory$', 'website.views.costHistory', name='costHistory') and the code it's: def costHistory(request): if validateToken(request): table = BillTable(Bill.objects.all()) appCosts = AppCost.objects.filter(userApp=request.session['stratosUser']) for app in appCosts: tarification = Tarification.objects.filter(id=app.tarification_id).get() application = Application.objects.filter(id=app.application_id).get() table.appName = application.nameApp table.userApp = app.userApp table.tarifName = tarification.nameTarif table.tarifCost = tarification.cost table.startTime = app.startTime table.finishTime = app.finishTime table.totalCost = app.totalCost RequestConfig(request, paginate={'per_page': 10}).configure(table) return render_to_response('menu/costHistory.html', {'table': table}, context_instance=RequestContext(request)) else: return render_to_response('menu/access_error.html') The second table it's on earningsHistory, the url it's url(r'^earningsHistory/$', 'website.views.earningsHistory', name='earningsHistory'), and the code it's: def earningsHistory(request): if validateToken(request): etable = BillTable(Bill.objects.all()) etable.exclude = ('userApp', 'startTime', 'finishTime',) appsAuthor = UploadApp.objects.filter(userApp=request.session['stratosUser']).values('application_id') for appId in appsAuthor: id = appId['application_id'] totalProfit = AppCost.objects.filter(application_id=id).aggregate(Sum('totalCost')) appTarif = UploadApp.objects.filter(userApp=request.session['stratosUser'], application_id=id).values('tarification_id').get() idTarif = appTarif['tarification_id'] application = Application.objects.filter(id=id).get() tarification = Tarification.objects.filter(id=idTarif).get() etable.appName = application.nameApp etable.tarifName = tarification.nameTarif etable.tarifCost = tarification.cost etable.totalCost = totalProfit['totalCost__sum'] RequestConfig(request, paginate={'per_page': 10}).configure(etable) return render_to_response('menu/earningsHistory.html', {'table': etable}, context_instance=RequestContext(request)) else: return render_to_response('menu/access_error.html') And the problem it's that on the second table I got displayed the same data as on the first table. Instead of getting 1 row I get … -
Django user_passes_test usage
I have a function-based view in Django: @login_required def bout_log_update(request, pk): ... While it's protected from people who aren't logged in, I need to be able to restrict access to this view based on: 1. The user currently logged in 2. Which user created the object (referred to by pk) It needs to be accessible only if the currently logged in user created the object being accessed, or is a superuser. Can the standard @user_passes_test decorator accomplish this? Or a custom decorator? Or another method entirely? I'd re-write it as a class-based view and use UserPassesTestMixin if I could, but I don't know that it's possible for this particular view. -
Django form.clean() run before field validators
https://docs.djangoproject.com/en/1.10/ref/forms/validation/ States that run_validators() is run before the form subclass’s clean(). My model looks like: def validate_ascii(value): try: value.encode('ascii') except UnicodeEncodeError: raise ValidationError("Contains non-ascii characters") class Keyword(models.Model): name = models.CharField(max_length=50, unique=True, validators=[validate_ascii]) In my form's clean() method, it finishes and calls return super(ModelForm, self).clean() After that, the validators for each field in the form is run. This is causing issues because my clean method assumes each field has had the validator run first and crashes. Why is my form's clean() method being run before the validators on the field? -
django makemigrations dose not create the model by order in models.py
all, I have a question about the order when creating the tables in django. When I create model with order of A, B and C in models.py. And I notice that, when run: python manage.py makemigrations app There is the migration file generate to create all the models, but the order is: - Create model B - Create model C - Create model A - Add field a_name to b. As the order in models.py really matters, but why the makemigrations dose not follow the order? Thanks very much -
Determine to what range curent date belongs Django
I have a different time ranges in my model period Period How can I filter out to what range current date belongs? I understand I should use range https://docs.djangoproject.com/en/1.7/ref/models/querysets/#range But is there one liner with query filter or I should be looping on all the model? -
Django CORS, HTTPS and WSGI setup
I am trying to setup CORS + HTTPS with mod_wsgi and Django but I can not get it to work. CORS works properly without HTTPS/mod_wsgi but stops working when I try to add HTTPS/mod_wsgi. For CORS, I use the Django CORS middleware (https://github.com/zestedesavoir/django-cors-middleware) My Django middleware_classes is the following: (see CorsMiddleware and CorsPostCsrfMiddleware) MIDDLEWARE_CLASSES = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'corsheaders.middleware.CorsMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', 'corsheaders.middleware.CorsPostCsrfMiddleware', ] I also add the following configuration: CORS_ALLOW_CREDENTIALS = True CORS_ORIGIN_ALLOW_ALL = True I run the Django/wsgi server as follow: python manage.py runmodwsgi --host 0.0.0.0 --port 8001 --https-port 8000 --ssl-certificate-file ../utils/ssl_cert/local.crt --ssl-certificate-key-file ../utils/ssl_cert/local.key --processes 8 --server-name localhost --https-only --reload-on-changes Successfully ran command. Server URL : http://localhost:8001/ Server URL (HTTPS) : https://localhost:8000/ Server Root : /tmp/mod_wsgi-0.0.0.0:8001:1000 Server Conf : /tmp/mod_wsgi-0.0.0.0:8001:1000/httpd.conf Error Log File : /tmp/mod_wsgi-0.0.0.0:8001:1000/error_log (warn) Request Capacity : 40 (8 processes * 5 threads) Request Timeout : 60 (seconds) Startup Timeout : 15 (seconds) Queue Backlog : 100 (connections) Queue Timeout : 45 (seconds) Server Capacity : 85 (event/worker), 70 (prefork) Server Backlog : 500 (connections) Locale Setting : en_US.UTF-8 If I query the HTTPS server with httpie from command line, it works as expected (no CORS issues of course). If I query the … -
Django configuration for errbit
We would like to send errors from Django application to errbit. On the web, there is no single example what to put in settings.py of web application.