Django community: Community blog posts RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
How to Create Django Signals
The Django Signals is a strategy to allow decoupled applications to get notified when certain events occur. Let’s say you want to invalidate a cached page everytime a given model instance is updated, but there are several places in your code base that this model can be updated. You can do that using signals, hooking some pieces of code to be executed everytime this specific model’s save method is trigged. Another common use case is when you have extended the Custom Django User by using the Profile strategy through a one-to-one relationship. What we usually do is use a “signal dispatcher” to listen for the User’s post_save event to also update the Profile instance as well. I’ve covered this case in another post, which you can read here: How to Extend Django User Model. In this tutorial I will present you the built-in signals and give you some general advices about the best practices. When Should I Use It? Before we move forward, know when you should use it: When many pieces of code may be interested in the same events; When you need to interact with a decoupled application, e.g.: A Django core model; A model defined by a … -
How to Return JSON-Encoded Response
Since version 1.7, Django counts with the built-in JsonResponse class, which is a subclass of HttpResponse. Its default Content-Type header is set to application/json, which is really convenient. It also comes with a JSON encoder, so you don’t need to serialize the data before returning the response object. See a minimal example below: from django.http import JsonResponse def profile(request): data = { 'name': 'Vitor', 'location': 'Finland', 'is_active': True, 'count': 28 } return JsonResponse(data) By default, the JsonResponse’s first parameter, data, should be a dict instance. To pass any other JSON-serializable object you must set the safe parameter to False. return JsonResponse([1, 2, 3, 4], safe=False) See below the class signature: class JsonResponse(data, encoder, safe, json_dumps_params, **kwargs) Defaults: data: (no default) encoder: django.core.serializers.json.DjangoJSONEncoder safe: True json_dumps_params: None Extra bits: If you want to return Django models as JSON, you may want to it this way: def get_users(request): users = User.objects.all().values('first_name', 'last_name') # or simply .values() to get all fields users_list = list(users) # important: convert the QuerySet to a list object return JsonResponse(users_list, safe=False) Troubleshooting Django 1.6 or below For older versions of Django, you must use an HttpResponse object. See an example below: import json from django.http import HttpResponse def … -
How to Return JSON-Encoded Response
Since version 1.7, Django counts with the built-in JsonResponse class, which is a subclass of HttpResponse. Its default Content-Type header is set to application/json, which is really convenient. It also comes with a JSON encoder, so you don’t need to serialize the data before returning the response object. See a minimal example below: from django.http import JsonResponse def profile(request): data = { 'name': 'Vitor', 'location': 'Finland', 'is_active': True, 'count': 28 } return JsonResponse(data) By default, the JsonResponse’s first parameter, data, should be a dict instance. To pass any other JSON-serializable object you must set the safe parameter to False. return JsonResponse([1, 2, 3, 4], safe=False) See below the class signature: class JsonResponse(data, encoder, safe, json_dumps_params, **kwargs) Defaults: data: (no default) encoder: django.core.serializers.json.DjangoJSONEncoder safe: True json_dumps_params: None Extra bits: If you want to return Django models as JSON, you may want to it this way: def get_users(request): users = User.objects.all().values('first_name', 'last_name') # or simply .values() to get all fields users_list = list(users) # important: convert the QuerySet to a list object return JsonResponse(users_list, safe=False) Troubleshooting Django 1.6 or below For older versions of Django, you must use an HttpResponse object. See an example below: import json from django.http import HttpResponse def … -
uWSGI Basic Django Setup
Here are two basic examples of almost the same uWSGI configuration to run a Django project; one is configured via an ini configuration file and the other is configured via a command line argument.This does not represent a production-ready example, but can be used as a starting point for the configuration.Setup for this example:# create dir for virtualenv and activatemkdir envs/virtualenv envs/runrun/. ./envs/runrun/bin/activate# create dir for project codebasemkdir proj/# install some django depspip install django uwsgi whitenose# create a new django projectcd proj/django-admin startproject runruncd runrun/# Add to or modify django settings.py to setup static file serving with Whitenoise.# Note: for prod environments, staticfiles can be served via Nginx.# settings.py MIDDLEWARE_CLASSES = [ .... 'whitenoise.middleware.WhiteNoiseMiddleware',] STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'Create the config file$ cat run.ini [uwsgi]module = runrun.wsgi:applicationvirtualenv = ../../envs/runrun/env = DJANGO_SETTINGS_MODULE=runrun.settings env = PYTHONPATH="$PYTHONPATH:./runrun/" master = trueprocesses = 5enable-threads = truemax-requests = 5000harakiri = 20vacuum = truehttp = :7777stats = 127.0.0.1:9191Execute configuwsgi --ini run.iniOr create a shell script$ cat run.sh uwsgi --module=runrun.wsgi:application \ -H ../../envs/runrun/ \ --env DJANGO_SETTINGS_MODULE=runrun.settings \ --env PYTHONPATH="$PYTHONPATH:./runrun/" \ --master \ --processes=5 \ --max-requests=5000 \ --harakiri=20 \ --vacuum \ … -
How to Reset Migrations
./manage.py migrate --fake <app-name> zero -
How to Reset Migrations
The Django migration system was developed and optmized to work with large number of migrations. Generally you shouldn’t mind to keep a big amount of models migrations in your code base. Even though sometimes it causes some undesired effects, like consuming much time while running the tests. But in scenarios like this you can easily disable the migrations (although there is no built-in option for that at the moment). Anyway, if you want to perform a clean-up, I will present a few options in this tutorial. Scenario 1: The project is still in the development environment and you want to perform a full clean up. You don’t mind throwing the whole database away. 1. Remove the all migrations files within your project Go through each of your projects apps migration folder and remove everything inside, except the __init__.py file. Or if you are using a unix-like OS you can run the following script (inside your project dir): find . -path "*/migrations/*.py" -not -name "__init__.py" -delete find . -path "*/migrations/*.pyc" -delete 2. Drop the current database, or delete the db.sqlite3 if it is your case. 3. Create the initial migrations and generate the database schema: python manage.py makemigrations python manage.py migrate … -
ShipIt Day Recap - July 2016
We finished up last week with another successful ShipIt Day. ShipIt Days are quarterly events where we put down client work for a little bit and focus on learning, stretching ourselves, and sharing. Everyone chooses to work together or individually on an itch or a project that has been on the back of their mind for the last few months. This time, we stretched ourselves by trying out new frameworks, languages, and pluggable apps. Here are some of the projects we worked on during ShipIt Day: TinyPNG Image Optimization in Django Kia and Dmitriy started on django_tinypng. This project creates a new OptimizedImageField in Django which uses the tinify client for the tinypng project to compress PNG files. This means that files uploaded by users can be reduced in size by up to 70% without perceivable differences in image quality. Reducing image sizes can free up disk space on servers and improve page load speeds, significantly improving user experiences. Maintaining Clean Python Dependencies / Requirements.txts Rebecca Muraya researched how we, as developers, can consistently manage our requirements files. In particular, she was looking for a way to handle second-level (and below) dependencies -- should these be explicitly pinned, or not? … -
SQLAlchemy and "Lost connection to MySQL server during query"
... a weird behaviour when pool_recycle is ignored Preface: This is quite a standard problem for apps/websites with low traffic or those using heavy caching and hitting the database quite seldom. Most of the articles you will find on the topic will tell you one thing - change the wait_timeout setting in the database. Unfortunately in some of the cases this disconnect occurs much earlier than the expected wait_timeout (default ot 8 hours). If you are in one of those cases keep reading. This issue haunted our team for weeks. When we first faced it the project that was raising it was still in dev phase so it wasn't critical but with getting closer to the release data we started to search for solution. We have read several articles and decided that pool_recycle is our solution. Adding pool_recycle: According to SQL Alchemy's documentation pool_recycle "causes the pool to recycle connections after the given number of seconds has passed". Nice, so if you recycle the connection in intervals smaller that the await_timeout the error above should not appear. Let's try it out: import time from sqlalchemy.engine import create_engine url = 'mysql+pymysql://user:pass@127.0.0.1:3306/db' engine = create_engine(url, pool_recycle=1).connect() query = 'SELECT NOW();' while True: … -
Django Tips #8 Blank or Null?
Django models API offers two similar options that usually cause confusion on many developers: null and blank. When I first started working with Django I couldn’t tell the difference and always ended up using both. Sometimes even using them improperly. Both do almost the same thing, as the name suggests, but here is the difference: Null: It is database-related. Defines if a given database column will accept null values or not. Blank: It is validation-related. It will be used during forms validation, when calling form.is_valid(). That being said, it is perfectly fine to have a field with null=True and blank=False. Meaning on the database level the field can be NULL, but in the application level it is a required field. Now, where most developers get it wrong: Defining null=True for string-based fields such as CharField and TextField. Avoid doing that. Otherwise, you will end up having two possible values for “no data”, that is: None and an empty string. Having two possible values for “no data” is redundant. The Django convention is to use the empty string, not NULL. So, if you want a string-based model field to be “nullable”, prefer doing that: class Person(models.Model): name = models.CharField(max_length=255) # Mandatory … -
Django Tips #8 Blank or Null?
Django models API offers two similar options that usually cause confusion on many developers: null and blank. When I first started working with Django I couldn’t tell the difference and always ended up using both. Sometimes even using them improperly. Both do almost the same thing, as the name suggests, but here is the difference: Null: It is database-related. Defines if a given database column will accept null values or not. Blank: It is validation-related. It will be used during forms validation, when calling form.is_valid(). That being said, it is perfectly fine to have a field with null=True and blank=False. Meaning on the database level the field can be NULL, but in the application level it is a required field. Now, where most developers get it wrong: Defining null=True for string-based fields such as CharField and TextField. Avoid doing that. Otherwise, you will end up having two possible values for “no data”, that is: None and an empty string. Having two possible values for “no data” is redundant. The Django convention is to use the empty string, not NULL. So, if you want a string-based model field to be “nullable”, prefer doing that: class Person(models.Model): name = models.CharField(max_length=255) # Mandatory … -
How to Extend Django User Model
The Django’s built-in authentication system is great. For the most part we can use it out-of-the-box, saving a lot of development and testing effort. It fits most of the use cases and is very safe. But sometimes we need to do some fine adjustment so to fit our Web application. Commonly we want to store a few more data related to our User. If your Web application have an social appeal, you might want to store a short bio, the location of the user, and other things like that. In this tutorial I will present the strategies you can use to simply extend the default Django User Model, so you don’t need to implement everything from scratch. Ways to Extend the Existing User Model Generally speaking, there are four different ways to extend the existing User model. Read below why and when to use them. Option 1: Using a Proxy Model What is a Proxy Model? It is a model inheritance without creating a new table in the database. It is used to change the behaviour of an existing model (e.g. default ordering, add new methods, etc.) without affecting the existing database schema. When should I use a Proxy Model? … -
How to Extend Django User Model
The Django’s built-in authentication system is great. For the most part we can use it out-of-the-box, saving a lot of development and testing effort. It fits most of the use cases and is very safe. But sometimes we need to do some fine adjustment so to fit our Web application. Commonly we want to store a few more data related to our User. If your Web application have an social appeal, you might want to store a short bio, the location of the user, and other things like that. In this tutorial I will present the strategies you can use to simply extend the default Django User Model, so you don’t need to implement everything from scratch. Ways to Extend the Existing User Model Generally speaking, there are four different ways to extend the existing User model. Read below why and when to use them. Option 1: Using a Proxy Model What is a Proxy Model? It is a model inheritance without creating a new table in the database. It is used to change the behaviour of an existing model (e.g. default ordering, add new methods, etc.) without affecting the existing database schema. When should I use a Proxy Model? … -
Django Tips #7 How to Get the Current URL Within a Django Template
Make sure you have django.template.context_processors.request listed in your context_processors. As of Django 1.9 version, it already comes configurated. The default TEMPLATES configuration looks like that: TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, }, ] To get the current path: {{ request.path }} Current path with querystring: {{ request.get_full_path }} Domain, path and querystring: {{ request.build_absolute_uri }} Outputs Considering we are acessing the following URL: http://127.0.0.1:8000/home/?q=test Method Output request.path /home/ request.get_full_path /home/?q=test request.build_absolute_uri http://127.0.0.1:8000/home/?q=test Troubleshooting Django 1.7 or below If you are using an older version of Django (<= 1.7) where the TEMPLATES configuration is not available, you can include the context processor like this: settings.py from django.conf.global_settings import TEMPLATE_CONTEXT_PROCESSORS as TCP TEMPLATE_CONTEXT_PROCESSORS = TCP + ( 'django.core.context_processors.request', ) Notice the context processor was available inside the core module. Since version >= 1.8 it is available inside the template module. -
Django Tips #7 How to Get the Current URL Within a Django Template
Make sure you have django.template.context_processors.request listed in your context_processors. As of Django 1.9 version, it already comes configurated. The default TEMPLATES configuration looks like that: TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, }, ] To get the current path: {{ request.path }} Current path with querystring: {{ request.get_full_path }} Domain, path and querystring: {{ request.build_absolute_uri }} Outputs Considering we are acessing the following URL: http://127.0.0.1:8000/home/?q=test Method Output request.path /home/ request.get_full_path /home/?q=test request.build_absolute_uri http://127.0.0.1:8000/home/?q=test Troubleshooting Django 1.7 or below If you are using an older version of Django (<= 1.7) where the TEMPLATES configuration is not available, you can include the context processor like this: settings.py from django.conf.global_settings import TEMPLATE_CONTEXT_PROCESSORS as TCP TEMPLATE_CONTEXT_PROCESSORS = TCP + ( 'django.core.context_processors.request', ) Notice the context processor was available inside the core module. Since version >= 1.8 it is available inside the template module. -
Sharing Media Files Without S3
Nowadays, it's common to deploy multiple application servers, but it poses a very common problem; How are these application servers going to share the media files contributed by the users? Cloud providers like Google, Rackspace or Amazon Web Services solve this problem by providing a cloud storage service. These services are comparable to an extremely slow hard disk that exposes an HTTP API. On the positive side, they are relatively cheap ($0.03/month per GB), provide redundant storage, and can be accessed from anywhere. Django’s storage abstraction layer, in combination with django-storages lets all your application servers manage files within the same bucket in the cloud. While emulating a server farm locally in containers, I looked into the options for sharing files without using a third-party service. NFS -- Network File System Network File System (NFS) is a distributed file system protocol originally developed by Sun Microsystems in 1984, allowing a user on a client computer to access files over the network much like local storage. This is the boring and battle-tested solution. In the simplest case, an NFS share can be created by installing the server from your package manager and adding a line to /etc/exports for each directory along … -
HTTP Status Codes Site
During the development of Simple Site Checker I realised that it would be useful for test purposes if there is a website returning all possible HTTP status codes. Thanks to Google App Engine and webapp2 framework building such website was a piece of cake. The site can be found at http://httpstatuscodes.appspot.com. The home page provides a list of all HTTP status codes and their names and if you want to get an HTTP response with a specific status code just add the code after the slash, example: http://httpstatuscodes.appspot.com/200 - returns 200 OK http://httpstatuscodes.appspot.com/500 - returns 500 Internal Server Error Also at the end of each page is located the URL of the HTTP protocol Status Codes Definitions with detailed explanation for each one of them. The website code is publicly available in github at HTTP Status Codes Site. If you find it useful feel free to comment and/or share it. -
Fabric & Django
Or how automate the creation of new projects with simple script Preface: Do you remember all this tiny little steps that you have to perform every time when you start new project - create virtual environment, install packages, start and setup Django project? Kind of annoying repetition, isn't it? How about to automate it a bit. Solution: Recently I started learning Fabric and thought "What better way to test it in practice than automating a simple, repetitive task?". So, lets mark the tasks that I want the script to perform: Create virtual environment with the project name Activate the virtual environment Download list of packages and install them Make 'src' directory where the project source will reside Create new Django project in source directory Update the settings Thanks to the local command the first one was easy. The problem was with the second one. Obviously each local command is run autonomously so I had to find some way have activated virtual environment for each task after this. Fortunately the prefix context manager works like a charm. I had some issues making it read and write in the paths I wants and voilà it was working exactly as I want. The … -
Python is not a Panacea ...
... neither is any other language or framework This post was inspired by the serial discussion on the topic "Python vs other language"(in the specific case the other one was PHP, and the question was asked in a Python group so you may guess whether there are any answers in favor of PHP). It is very simple, I believe that every Python developer will tell you that Python is the greatest language ever build, how easy is to learn it, how readable and flexible it is, how much fun it is to work with it and so on. They will tell you that you can do everything with it: web and desktop development, testing, automation, scientific simulations etc. But what most of them will forgot to tell you is that it is not a Panacea. In the matter of fact you can also build "ugly" and unstable applications in Python too. Most problems come not from the language or framework used, but from bad coding practices and bad understanding of the environment. Python will force you to write readable code but it wont solve all your problems. It is hard to make a complete list of what exactly you must … -
Django compressor and image preloading
Preface: Have you noticed how on some websites when you click on a link that opens a lightbox or any overlay for first time it takes some time to display the border/background/button images. Not quite fancy, right? This is because the load of this images starts at the moment the overlay is rendered on the screen. If this is your first load and these images are not in your browser cache it will take some time for the browser to retrieve them from the server. Solution: The solution for this is to preload the images i.e. to force the browser to request them from the server before they are actually used. With a simple javascript function and a list of the images URLs this is a piece of cake: $.preLoadImages = function() { var args_len = arguments.length; for (var i=0; i < args_len; i++) { var cacheImage = document.createElement('img'); cacheImage.src = arguments[i]; } } $.preLoadImages('/img/img1.png', '/img/img2.png') Please have in mind that the code above uses the jQuery library. Specialty: Pretty easy, but you have to hardcode the URLs of all images. Also if you are using Django compressor then probably you are aware that it adds extra hash to the … -
The road to hell is paved with regular expressions ...
... or what is the cost of using regular expressions for simple tasks Regular expressions are one of the most powerful tools in computing I have ever seen. My previous post about Django compressor and image preloading is a good example how useful they might be. The only limit of their use is your imagination. But "with great power, comes great responsibility" or in this case a great cost. Even the simplest expressions can be quite heavy compared with other methods. The reason to write about this is a question recently asked in a python group. It was about how to get the elements of a list that match specific string. My proposal was to use comprehension list and simple string comparison while other member proposed using a regular expression. I was pretty sure that the regular expression is slower but not sure exactly how much slower so I made a simple test to find out. import re import timeit my_list = ['abc-123', 'def-456', 'ghi-789', 'abc456', 'abc', 'abd'] def re_check(): return [i for i in my_list if re.match('^abc$', i)] t = timeit.Timer(re_check) print 're_check result >>', re_check() print "%.2f usec/pass" % (1000000 * t.timeit(number=100000)/100000) def simple_check(): return [i for i … -
Automated deployment with Ubuntu, Fabric and Django
A few months ago I started to play with Fabric and the result was a simple script that automates the creation of a new Django project. In the last months I continued my experiments and extended the script to a full stack for creation and deployment of Django projects. As the details behind the script like the project structure that I use and the server setup are a bit long I will keep this post only on the script usage and I will write a follow up one describing the project structure and server. So in a brief the setup that I use consist of Ubuntu as OS, Nginx as web server and uWSGI as application server. The last one is controlled by Upstart. The script is available for download at GitHub. In a wait for more detailed documentation here is a short description of the main tasks and what they do: startproject:<project_name> Creates a new virtual environment Installs the predefined packages(uWSGI, Django and South)) Creates a new Django project from the predefined template Creates different configuration files for development and production environment Initializes new git repository Prompts the user to choose a database type. Then installs database required packages, … -
Django project file structure
As I promised in Automated deployment with Ubuntu, Fabric and Django I will use this post to explain the file structure that I use for my Django projects and what I benefit from it. So here is my project directory tree. The structure ~/workspace/&lt;project_name&gt;/ |-- bin |-- include |-- lib |-- local |-- src |-- .git |-- .gitignore |-- required_packages.txt |-- media |-- static |-- &lt;project_name&gt; | |-- &lt;project_name&gt; | | |-- __init__.py | | |-- settings | | | |-- __init__.py | | | |-- &lt;environment_type&gt;.py | | | |-- local.py | | |-- templates | | |-- urls.py | | |-- views.py | |-- manage.py | |-- wsgi.py |-- &lt;project_name&gt;.development.nginx.local.conf |-- &lt;project_name&gt;.&lt; environment_type&gt;.nginx.uwsgi.conf |-- &lt;project_name&gt;.&lt; environment_type&gt;.uwsgi.conf Explanation At the top I have a directory named as the project and virtual environment inside of it. The benefit from it is complete isolation of the project from the surrounding projects and python packages installed at OS level and ability to install packages without administrator permissions. It also provides an easy way to transfer the project from one system to another using a requirements file. The src folder is where I keep everything that is going to enter the version control … -
Automation, Fabric and Django - presentation
As a follow up post of Automated deployment with Ubuntu, Fabric and Django here are the slides from my presentation on topic "Automation, Fabric and Django". Unfortunately there is no audio podcast but if there is interest I can add some comments about each slide as part of this post. Automation - fabric, django and more from Ilian Iliev If there is anything that need explanation feel free to ask. -
Simple Site Checker and the User-Agent header
Preface: Nine months ago(I can't believe it was that long) I created a script called Simple Site Checker to ease the check of sitemaps for broken links. The script code if publicly available at Github. Yesterday(now when I finally found time to finish this post it must be "A few weeks ago") I decided to run it again on this website and nothing happened - no errors, no warning, nothing. Setting the output level to DEBUG showed the following message "Loading sitemap ..." and exited. Here the fault was mine, I have missed a corner case in the error catching mechanism i.e. when the sitemap URL returns something different from "200 OK" or "500 internal server error". Just a few second and the mistake was fix. Problem and Solution: I ran the script again and what a surprise the sitemap URL was returning "403 Forbidden". At the same time the sitemap was perfectly accessible via my browser. After some thinking I remembered about that some security plugins block the access to the website if there is not User-Agent header supplied. The reason for this is to block the access of simple script. In my case even an empty User-Agent did … -
Functions in Python presentation
Here is my presentation part of the in company Python course. Functions in python from Ilian Iliev The last slide - "Problem to solve" is something like a simple homework. Sample solutions will be uploaded later this week.