Django community: RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
uWSGI File Wrapper and Python 3.5
A couple of weeks ago we hit a small but hard to track issue in one of our clients. They opened an issue telling us that the downloaded files appeared as corrupt. After closer examination, the downloaded files had 0 bytes. The HTTP response was a solid 200 OK with a 0 bytes file attachment. This was very strange, given that no automatic error reports had been triggered and there was clearly an error. Morover we noticed that this issue in particular happened whenever the backend Django app returned a file in the response. This is quite rare in a production environment, given that you most certainly don’t want the Django app handling files, but rather links to the files. However, one usual exception for this rule are small dynamic reports (for bigger ones you’ll want to send an email link when its ready). On the Django logs we could clearly see that the reports were properly created and no errors were present neither there nor in the nginx logs. That left us to take a peek at the application server logs, in this case, uWSGI. It was there that we got to see the following:io.UnsupportedOperation: fileno During handling of … -
Introducing django-perf-rec, our Django performance testing tool
N.B. This is a cross-post from the YPlan tech blog. During PyCon UK I had the opportunity to work on open-sourcing our in-house Django performance testing tool, which has now been released as django-perf-rec. We created it over two years ago, and have been using and improving it since. It has been helping us to pre-emptively fix performance problems in our code, and now it can help you! In the old days we used to often see performance regressions when we introduced a new feature to existing code, and we’d have to retroactively understand and fix them. For example, we might add a feature that accesses a new foreign key on a model, and because select_related / prefetch_related hadn’t been added to the appropriate QuerySet, we’d see an N+1 query problem appear. Often the problems would only manifest in real slowness in production, as our test and development environments don’t contain much data. We tried to lock these down with Django’s assertNumQueries (docs) in tests like: def test_books(self): with self.assertNumQueries(4): self.client.get('/books/') This worked on a basic level, but if the test failed on you, you’d be left with little information on what caused the failure, and be forced to manually … -
Django Settings Deployment
Learn how to deploy Django settings into environment independent such as development and production -
Building a multi-tenant web application with Django 1.9 framework(Part1)
In this tutorial we are going to build a multi tenant web application with Django 1.9 framework.Multi tenancy is used mainly for SAAS(Software As a Service) products.SAAS is a software delivery model used in the cloud where each customer gets only the service offered by a specific product not the product itself ,the customer gets a relatively isolated environment for its data which other customers don't have access to. Multitenancy can be achieved using 3 different techniques: Single web application instance and multiple database With this multitenancy technique, each customer gets his own database which offers the most secure environment for clients but also it is the most costly technique in term of servers resources. Single web application instance and multiple schemas With this multitenancy technique each client or tenant gets his own schema.Schemas are only available with Postgres database system. Single web application instance,one database and one schema With multitenancy technique tenants share the same database and the same schema ,the security and separation is handled only by application code.There is no real separation in client's data.This method is less secure but it takes less server resources. Multitenancy with Django and Postgres Each technique has its pros and cons.In … -
Building a multi-tenant web application with Django 1.9 framework(Part1)
In this tutorial we are going to build a multi tenant web application with Django 1.9 framework.Multi tenancy is used mainly for SAAS(Software As a Service) products.SAAS is a software delivery model used in the cloud where each customer gets only the service offered by a specific product not the product itself ,the customer gets a relatively isolated environment for its data which other customers don't have access to. Multitenancy can be achieved using 3 different techniques: Single web application instance and multiple database With this multitenancy technique, each customer gets his own database which offers the most secure environment for clients but also it is the most costly technique in term of servers resources. Single web application instance and multiple schemas With this multitenancy technique each client or tenant gets his own schema.Schemas are only available with Postgres database system. Single web application instance,one database and one schema With multitenancy technique tenants share the same database and the same schema ,the security and separation is handled only by application code.There is no real separation in client's data.This method is less secure but it takes less server resources. Multitenancy with Django and Postgres Each technique has its pros and cons.In … -
How to Create a Password Reset View
For this short tutorial we will be using the django.contrib.auth views to add a password reset functionality to your Django application. The process of reseting passwords involves sending emails. For that matter we will be using console email backend to debug and check if everything is working. In the end of this tutorial I will also provide resources to properly configure a prodution-quality email server. Dependencies Basically all you need is to have django.contrib.auth in your INSTALLED_APPS and a email service properly configurated (for production). During the development we can use file/console email backend. settings.py INSTALLED_APPS = [ ... 'django.contrib.auth', ] EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' # During development only Implementing We need 4 different views: password_reset: Form where the user submit the email address password_reset_done: Page displayed to the user after submitting the email form. Usually with instructions to open the email account, look in the spam folder etc. And asking for the user to click on the link he will receive. password_reset_confirm: The link that was emailed to the user. This view will validate the token and display a password form if the token is valid or an error message if the token is invalid (e.g. was already used or … -
Caktus Group @ PyData Carolinas 2016
Tomorrow marks the official beginning of PyData Carolinas 2016 (though technically, the tutorials started today). This is the first time PyData has hosted a conference in our area. We’re especially proud of the way local leaders and members of meetups like TriPython, TechGirlz, Girl Develop It RDU, and PyLadies have worked in tandem to put this event together for the Python community. -
Performing Maintenance on Pods
Kubernetes includes a feature called services which serve as a kind of load balancer for pods. When pods misbehave or otherwise stop working, sometimes you'll want to remove the pod from the service without killing the pod. ## Services & Endpoints Each service has a list of endpoints for the service which correspond to the pods for that service. This list of endpoints is updated automatically with the IPs and ports for the pods based on a label selector defined on the service. This allows the service to be loosely coupled to the pods themselves. You can see the selector pattern used in [...] -
Django Shortcuts
This module is a collection of helper classes generally used in view functions/classes. All the shortcuts are available in the module django.shortcuts. render Params: def render(request, template_name, context=None, content_type=None, status=None, using=None) Shortcut for: content = loader.render_to_string(template_name, context, request, using=using) return HttpResponse(content, content_type, status) There is also render_to_response, the only difference is that it does not pass the request to the context. redirect Params: def redirect(to, *args, **kwargs): Returns an HttpResponseRedirect (or HttpResponsePermanentRedirect) to the appropriate URL for the arguments passed. The arguments could be: A model: the model’s get_absolute_url() function will be called. A view name, possibly with arguments: urls.reverse() will be used to reverse-resolve the name. A URL, which will be used as-is for the redirect location. Shortcut for: def post_view(request, post_id): post = Post.objects.get(pk=post_id) return redirect(post) # equivalent to: return HttpResponseRedirect(post.get_absolute_url()) def post_view(request, post_id): return redirect('post_details', id=post_id) # equivalent to: return HttpResponseRedirect(reverse('post_details', args=(post_id, ))) def relative_url_view(request): return redirect('/posts/archive/') # equivalent to: return HttpResponseRedirect('/posts/archive/') def absolute_url_view(request): return redirect('https://simpleblog.com/posts/archive/') # equivalent to: return HttpResponseRedirect('https://simpleblog.com/posts/archive/') See more in this post about the redirect function. get_object_or_404 Params: def get_object_or_404(klass, *args, **kwargs): Shortcut for: try: return Model.objects.get(pk=1) except Model.DoesNotExist: raise Http404() get_list_or_404 Params: def get_list_or_404(klass, *args, **kwargs): Shortcut for: obj_list = list(Model.objects.filter(title='test')) if … -
Watermarking images on Django sites
Have you ever noticed how stock photography sites add watermarks to the images shown on their catalogs? They do that to make sure people don’t just take the free samples and use them without proper licensing. Google Maps does it for the satellite imagery as well. Turns out this is pretty easy to do and we’ll show you how to do it for Django sites sites using Pillow and django-imagekit. All the code is available on GitHub and a Vagrantfile is provided if you want to try the live Django demo by yourself. Watermarking images First we’ll show you three simple watermarking techniques that can be used in an Python project that uses images, and then we’ll use what we’ve built to add watermarks to images on a sample Django site. Text overlay The first thing we’ll do is add a semi-transparent text legend on the center of an image. We’ll use Pillow’s Image, ImageDraw and ImageFont modules. The following function adds a text overlay on the center of the supplied image: # processors.py from PIL import Image, ImageDraw, ImageFont _default_font = ImageFont.truetype('/usr/share/fonts/dejavu/DejaVuSans-Bold.ttf', 24) def add_text_overlay(image, text, font=_default_font): rgba_image = image.convert('RGBA') text_overlay = Image.new('RGBA', rgba_image.size, (255, 255, 255, 0)) image_draw … -
A Brief Introduction to Django Channels
Django has long been an excellent web framework. It has helped many developers and numerous businesses succeed over the years. But before the introduction of Channels, Django only supported the http protocol well. With the gradual evolution of the web technologies, standing here in 2016, supporting http only is simply not enough. Today, we are using websockets for real time communications, WebRTC is getting popular for real time collaboration or video calling, HTTP/2 is also being adapted by many. In the current state of the web, any modern web framework needs to be able to support more and more protocols. This is where Django Channels come into play. Channels aim at adding new capabilities to Django, including the support for modern web technologies like websockets or http2. How does “Channels” work? The idea behind Channels is quite simple. To understand the concept, let’s first walk through an example scenario, let’s see how Channels would process a request. A http/websocket request hits the reverse proxy (ie, nginx). This step is not compulsory but we’re conscious developers and always make sure our requests first go through a hardened, battle proven reverse proxy before it hits our application server Nginx passes the request … -
Testing dates in Django
Django makes unit & functional testing easy (especially with WebTest). Tests on routing, permissions, database updates and emails are all straightforward to implement but how do you test dates & time? You might for example want to test regular email notifications. -
Testing dates in Django
Django makes unit & functional testing easy (especially with WebTest). Tests on routing, permissions, database updates and emails are all straightforward to implement but how do you test dates & time? You might for example want to test regular email notifications. -
Fixing "Symbol not found: _PyUnicodeUCS2_Compare and _PyUnicodeUCS2_AsASCIIString"
I've got a new client with a legacy Django project running on Python 2.7 with Pandas. When I was configuring my local OS X environment I got a bunch of nasty errors: ImportError: dlopen(/Users/mike/.pyenv/versions/project/lib/python2.7/site-packages/pandas/hashtable.so, 2): Symbol not found: _PyUnicodeUCS2_Compare Referenced from: /Users/mike/.pyenv/versions/project/lib/python2.7/site-packages/pandas/hashtable.so Expected in: flat namespace in /Users/mike/.pyenv/versions/project/lib/python2.7/site-packages/pandas/hashtable.so My first assumption was to upgrade Pandas from 0.15.1 to the most recent 0.18.1, but that didn't help. I rolled back to Pandas 0.15.1 to avoid any possible regressions. After tens minutes of reading traceback messages and googling, I managed to fix that error with running $ pip install -U cython. After that I've got: ImportError: dlopen(/Users/mike/.pyenv/versions/2.7.12/envs/project/lib/python2.7/site-packages/numpy/core/multiarray.so, 2): Symbol not found: _PyUnicodeUCS2_AsASCIIString Referenced from: /Users/mike/.pyenv/versions/2.7.12/envs/project/lib/python2.7/site-packages/numpy/core/multiarray.so Expected in: flat namespace in /Users/mike/.pyenv/versions/2.7.12/envs/project/lib/python2.7/site-packages/numpy/core/multiarray.so It seemed to be fixed in the most recent Numpy version, so I ran $ pip install -U numpy pandas, and the error disappeared. I hope this short blog post will save your time. You're welcome to share your experience with fixing similar not obvious issues. -
Responsive Thumbnails in Django Templates with sorl-thumbnail
A 2026 guide to responsive images in Django with sorl-thumbnail and Pillow: generate multiple sizes with the {% thumbnail %} tag, wire up srcset/sizes and the element for art direction and retina, serve WebP and AVIF, lazy-load, and set width/height to protect Core Web Vitals. Includes a feature comparison with easy-thumbnails, django-imagekit, and image CDNs. -
Django Tips #14 Using the Messages Framework
Keeping the users of your application aware of what is going on makes a huge difference in the user experience. If there is something users hate more than slow applications, it is applications that does not communicate with them. – The user clicks in a save button. – Nothing happens. – So, did it save the data or not? – User reaction after a couple of (mili)seconds: *Click!* *Click!* *Click!* *Click!* Let’s make our users more confident and comfortable, shall we? Configuration By default, a brand new Django project already comes with the messages framework installed. If you did not change anything in relation to the messages framework, just skip to the next section. Otherwise, set it up: INSTALLED_APPS django.contrib.messages MIDDLEWARE or MIDDLEWARE_CLASSES in older versions: django.contrib.sessions.middleware.SessionMiddleware django.contrib.messages.middleware.MessageMiddleware TEMPLATES context_processors django.contrib.messages.context_processors.messages Message Levels and Tags Constant Level Tag (for CSS) Purpose DEBUG 10 debug Development-related messages that will be ignored (or removed) in a production deployment INFO 20 info Informational messages for the user SUCCESS 25 success An action was successful WARNING 30 warning A failure did not occur but may be imminent ERROR 40 error An action was not successful or some other failure occurred By default, Django will … -
Django Celery + Social Auth: Async Pipeline Best Practices
Keep Django social login fast by offloading slow pipeline steps — avatar fetch, profile sync, welcome email — to Celery 5.x tasks, using the maintained social-auth-app-django package. -
How to Create Periodic Tasks in Django with Celery Beat
A practical, up-to-date guide to running scheduled and recurring background tasks in Django using Celery 5.x and Celery Beat: app setup, crontab and interval schedules, runtime-editable schedules with django-celery-beat, and production tips. -
Custom Error Pages in Django (404, 500, 403, 400)
When DEBUG is False, Django serves your own 404, 500, 403 and 400 pages. Here's how to wire up the templates and handler views, dodge the 500 context gotcha, and test them locally in Django 5.x. -
Deploying Django with Gunicorn and Supervisor
Here in Monmar we deploy all Django applications with Gunicorn and Supervisor. I personally prefer Gunicorn to uWSGI because it has better configuration options and more predictable performance. In this article we will be deploying a typical Django application. We won't be using async workers because we're just serving HTML and there are no heavy-lifting task in background. I'm assuming that our Django project was created with Cookiecutter Django, an awesome boilerplate developer by Daniel Roy Greenfeld. I strongly recommend using it for every new Django project. Our requirements/production.txt file should already contain these two packages: # WSGI Handler # ------------------------------------------------ gevent==1.1.1 gunicorn==19.6.0 So, we only need to SSH to the server, create a virtualenv and populate it with packages: $ virtualenv --python=python3.5 .venv $ source .venv/bin/activate $ pip3 install -r requirements.txt Next, we're going to add missing configs to our working copy: $ mkdir -p deploy/production && cd deploy/production $ touch gunicorn.conf.py nginx.conf run.sh supervisor.conf I'm providing real configuration files we're using for our company website. So, please update it for your project needs. gunicorn.conf.py bind = 'unix:/tmp/gunicorn-monmar.sock' workers = 2 timeout = 30 nginx.conf upstream monmar { server unix:/tmp/gunicorn-monmar.sock fail_timeout=0; keepalive 60; } server { listen 80; listen … -
How to Work With AJAX Request With Django
There are many scenarios where you may want to use AJAX requests in your web application. It is a great resource that enables web applications to be faster and more dynamic. For this short tutorial I will be using jQuery to ease the implementation. You can choose a different framework, or even implement it using JavaScript only. But the concept will remain the same. Initial Setup Here is how my base.html template looks like: {% load static %}<!doctype html> <html> <head> <meta charset="utf-8"> <title>{% block title %}Default Title{% endblock %}</title> <link rel="stylesheet" type="text/css" href="{% static 'css/app.css' %}"> {% block stylesheet %}{% endblock %} </head> <body> <header> ... </header> <main> {% block content %} {% endblock %} </main> <footer> ... </footer> <script src="https://code.jquery.com/jquery-3.1.0.min.js"></script> <script src="{% static 'js/app.js' %}"></script> {% block javascript %}{% endblock %} </body> </html> The jQuery library and all the JavaScript resources stays in the end of the HTML page for two reasons: to guarantee the DOM will be loaded whe the script is executed and to avoid inline scripts (at least scripts that uses jQuery). All the extra or page specific JavaScript goes inside the {% block javascript %}{% endblock %} block. Sample Scenario Let’s say you want … -
Django to Ember #1
If you know Django and want to learn Ember (or vice-versa), this guide is for you. To get started, let's see what happens when a user opens the `/contact` page in both frameworks. -
Django to Ember #1
If you know Django and want to learn Ember (or vice-versa), this guide is for you. To get started, let's see what happens when a user opens the `/contact` page in both frameworks. -
Simple qgis plugin repo
At my company, we sometimes build QGIS plugins. You can install those by hand by unzipping a zipfile in the correct directory, but there's a nicer way. You can add custom plugin "registries" to QGIS and QGIS will then treat your plugins just like regular ones. Here's an example registry: https://plugins.lizard.net/ . Simple, right? Just a directory on our webserver. The URL you have to configure inside QGIS as registry is that of the plugins.xml file: https://plugins.lizard.net/plugins.xml . The plugins.xml has a specific format: <?xml version="1.0"?> <plugins> <pyqgis_plugin name="GGMN lizard integration" version="1.6"> <description>Download GGMN data from lizard, interpolate and add new points</description> <homepage>https://github.com/nens/ggmn-qgis</homepage> <qgis_minimum_version>2.8</qgis_minimum_version> <file_name>LizardDownloader.1.6.zip</file_name> <author_name>Reinout van Rees, Nelen &amp; Schuurmans</author_name> <download_url>https://plugins.lizard.net/LizardDownloader.1.6.zip</download_url> </pyqgis_plugin> .... more plugins ... </plugins> As you see, the format is reasonably simple. There's one directory on the webserver that I "scp" the zipfiles with the plugins to. I then run this script on the directory. That script extracts the (mandatory) metadata.txt from all zipfiles and creates a plugins.xml file out of it. A gotcha regarding the zipfiles: they should contain the version number, but, in contrast to python packages, the version should be prefixed by a dot instead of a dash. So no myplugin-1.0.zip but myplugin.1.0.zip. … -
How to Create a One Time Link
Django uses a very interesting approach to generate the Password reset tokens. I’m not really a security expert, neither I’m very familiar with cryptography algorithms, but it is very safe and reliable. Before I elaborate a little be more on the one-time-link generation, I wanted to discuss about the Django’s PasswordResetTokenGenerator implementation. Because what we will be doing is actually extending this particular class to fit our needs. Generally speaking, Django generate a token without persisting it in the database. Yet, it still have the capabilities of determining whether a given token is valid or not. Also the token is only valid for a defined number of days. The default value for the Password Reset Token is 7 days, and it can be changed in the settings.py by changing the value of PASSWORD_RESET_TIMEOUT_DAYS. The class have two public methods: make_token(user) check_token(user, token) The make_token method will generate a hash value with user related data that will change after the password reset. Meaning, after the user clicks on the link with the hash and proceed to the password reset, the link (or the hash) will no longer be valid: def _make_hash_value(self, user, timestamp): # Ensure results are consistent across DB backends …