Django community: RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
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. -
Functions in Python Homework
After some delay(please excuse me for it) here are sample solutions of the problems defined in the presentation Functions in Python. Solutions are also available at GitHub. -
Introduction to Django - presentation
This presentation shows the basics of Django - what is inside the framework and explains the Model-View-Template system. One of the most important parts is a diagram how the request is processed and the response is generated. Shows the project and the application structure and the basic elements - Models, URLs dispatcher, Views and Templates. Introduction to django from Ilian Iliev -
Django for Web Prototyping
Or how to use the benefits of Django template system during the PSD to HTML phase There are two main approaches to start designing a new project - Photoshop mock-up or an HTML prototype. The first one is more traditional and well established in the web industry. The second one is more alternative and (maybe)modern. I remember a video of Jason Fried from 37 Signals where he talks about design and creativity. You can see it at http://davegray.nextslide.com/jason-fried-on-design. There he explains how he stays away from the Photoshop in the initial phase to concetrate on the things that you can interact with instead of focusing on design details. I am not planning to argue which is the better method, the important thing here is that sooner or later you get to the point where you have to start the HTML coding. Unfortunately frequently this happens in a pure HTML/CSS environment outside of the Django project and then we waste some extra amount of time to convert it to Django templates. Wouldn't be awesome if you can give the front-end developers something that they can install/run with a simple command and still to allow them to work in the Django environment … -
Python Interview Question and Answers
Update 2: You can also check the follow up post Django interview questions. For the last few weeks I have been interviewing several people for Python/Django developers so I thought that it might be helpful to show the questions I am asking together with the answers. The reason is ... OK, let me tell you a story first. I remember when one of my university professors introduced to us his professor - the one who thought him. It was a really short visit but I still remember one if the things he said. "Ignorance is not bad, the bad thing is when you do no want to learn." So back to the reason - if you have at least taken care to prepare for the interview, look for a standard questions and their answers and learn them this is a good start. Answering these question may not get you the job you are applying for but learning them will give you some valuable knowledge about Python. This post will include the questions that are Python specific and I'll post the Django question separately. How are arguments passed - by reference of by value? The short answer is "neither", actually it … -
Resurection
Well, as some of you may have seen this blog was on hold for quite a long time. There were multiple reasons mainly my Ph.D. and changing my job but it is back online. So, what is new? As a start this blog is no longer running on wordpress. The reason is that I had some issues with wordpress - the blog was hacked twice due to security holes in wordpress/plugins, it was terribly slow and the code looked like shit. Lots of inline styles and javascript etc. So I made a simple Django based blog that generates static content. Alse we have new design and new domain, the last one much easier to remember ))) Also the comments are now handled by Disquss and the search functinality is provided by Google. The code of the blog, needs some minor cleaning and then it will be released publicly in the next few weeks. Meanwhile you can check my latest post Working with intervals in Python. P.S. I have finally finished my Ph.D. so no more university/reasearch job and hopefully more time for blogging. -
Working with intervals in Python
Brief: Working with intervals in Python is really easy, fast and simple. If you want to learn more just keep reading. Task description: Lets say that the case if the following, you have multiple users and each one of them has achieved different number of points on your website. So you want, to know how many users haven't got any point, how many made between 1 and 50 points, how many between 51 and 100 etc. In addition at 1000 the intervals start increasing by 100 instead of 50. Preparing the intervals: Working with lists in Python is so awesome, so creating the intervals is quite a simple task. intervals = [0] + \ # The zero intervals [x * 50 for x in range(1, 20)] + \ # The 50 intervals [x * 100 for x in range(10, 100)] + \ # The 100 intervals [x * 1000 for x in range(10, 102)] # the 1000 intervals So after running the code above we will have a list with the maximum number of points for each interval. Now it is time to prepare the different buckets that will store the users count. To ease this we are going to … -
Django, pytz, NonExistentTimeError and AmbiguousTimeError
Brief: In one of the project I work on we had to convert some old naive datetime objects to timezone aware ones. Converting naive datetime to timezone aware one is usually a straightforward job. In django you even have a nice utility function for this. For example: import pytz from django.utils import timezone timezone.make_aware(datetime.datetime(2012, 3, 25, 3, 52), timezone=pytz.timezone('Europe/Stockholm')) # returns datetime.datetime(2012, 3, 25, 3, 52, tzinfo=<DstTzInfo 'Europe/Stockholm' CEST+2:00:00 DST>) Problem: You can use this for quite a long time until one day you end up with something like this: timezone.make_aware(datetime.datetime(2012, 3, 25, 2, 52), timezone=pytz.timezone('Europe/Stockholm')) # which leads to Traceback (most recent call last): File "", line 1, in File "/home/ilian/venvs/test/lib/python3.4/site-packages/django/utils/timezone.py", line 358, in make_aware return timezone.localize(value, is_dst=None) File "/home/ilian/venvs/test/lib/python3.4/site-packages/pytz/tzinfo.py", line 327, in localize raise NonExistentTimeError(dt) pytz.exceptions.NonExistentTimeError: 2012-03-25 02:52:00 Or this: timezone.make_aware(datetime.datetime(2012, 10, 28, 2, 52), timezone=pytz.timezone('Europe/Stockholm')) #throws Traceback (most recent call last): File "", line 1, in File "/home/ilian/venvs/test/lib/python3.4/site-packages/django/utils/timezone.py", line 358, in make_aware return timezone.localize(value, is_dst=None) File "/home/ilian/venvs/test/lib/python3.4/site-packages/pytz/tzinfo.py", line 349, in localize raise AmbiguousTimeError(dt) pytz.exceptions.AmbiguousTimeError: 2012-10-28 02:52:00 Explanation: The reason for the first error is that in the real world this datetime does not exists. Due to the DST change on this date the clock jumps from 01:59 …