Good Django resources

This page provides some good Django resources.

Good posts about Django

  • Don’t repeat Yourself – Understanding Django Template Inheritance

Don’t repeat Yourself – Understanding Django Template Inheritance [April 17, 2018 by Daniel Hepper, PDF]


  • Django is too complicated!

Django is too complicated! [August 14, 2018 by Daniel Hepper]


Good posts about Django updates

  • Django 2.0 url() to path() cheatsheet

Django 2.0 url() to path() cheatsheet [May 2, 2018 by Daniel Hepper, PDF]

Django 2.0 introduced a new way to define URLs, which greatly simplifies how parameters are captured.

In earlier versions of Django, you had to use the url() method and pass a regular expressions with named capturing groups to capture URL parameters.

In Django 2.0, you use the path() method with path converters to capture URL parameters.

path() always matches the complete path, so path('account/login/') is equivalent to url('^account/login/$').

use re_path(), which is a drop-in replacement for the old url() method.

from django.urls import re_path
re_path(r'^.*', TemplateView.as_view(template_name = "home.html")),

Also, the newpath() method does not support positional arguments, you must provide a name.
So, assuming the argument to your view is called id (something like def detail(request, id)), your URL definition would look like this:
path('', views.detail, name=’detail’)
Alternatively, you could just use re_path() instead of url():
re_path(r’^[0-9]+)$’, views.detail, name=’detail’)

Don’t repeat Yourself – Understanding Django Template Inheritance


References and Reading List






How to Deploy a Django Application on a Server Running Ubuntu

This post provides tutorials on how to deploy a Django application on a server running Ubuntu.

Softwares needed to set up a server for Django application:

  • Virtualenv & Virtualenvwrapper / Conda
  • Django
  • Gunicorn
  • Nginx
  • Supervisor


Nginx will face the outside world. It will serve media files (images, CSS, etc.) directly from the file system. However, it can’t talk directly to Django applications; it needs something that will run the application, feed it requests from the web, and return responses.

That’s Gunicorn‘s job. Gunicorn will create a Unix socket, and serve responses to Nginx via the wsgi protocol – the socket passes data in both directions:

The outside world <-> Nginx <-> The socket <-> Gunicorn

All this family will live into a Virtualenv. Already wondered why Virtualenv is so useful when you develop Pythons’s applications? Continue to read and you will understand.

Before you start


I assume you have a server available on which you have root privileges. I am using a server running Debian 7, so everything here should also work on an Ubuntu server or other Debian-based distribution. If you’re using an RPM-based distro (such as CentOS), you will need to replace the aptitude commands by their yum counterparts and if you’re using FreeBSD you can install the components from ports.

Import CSV using Pandas to Django models

This post introduces how to import CSV data using Pandas to Django models.

Python has a built-in csv library, but do not use that, it is not flexible for csv data that has both string and number based data. See the reasons below:

builtin csv module is very primitive at handling mixed data-types, does all its type conversion at import-time, and even at that has a very restrictive menu of options, which will mangle most real-world datasets (inconsistent quoting and escaping, missing or incomplete values in Booleans and factors, mismatched Unicode encoding resulting in phantom quote or escape characters inside fields, incomplete lines will cause exception). Fixing csv import is one of countless benefits of pandas. So, your ultimate answer is indeed stop using builtin csv import and start using pandas.

Do not import the data in csv file to Django models via row by row method– that is too slow.

Django (version > 1.4 )  provides  bulk_create as an object manager method which takes as input an array of objects created using the class constructor.

See my example code below:

import pandas as pd



row_iter = df.iterrows()

objs = [


        field_1 = row['Name'],

        field_2  = row['Description'],

        field_3  = row['Notes'],

        field_4  = row['Votes']


    for index, row in row_iter



#Note: myClass_in_model: the class (i.e., the table you want to populate data from csv) we defined in Django
#Note: field_1 to filed_4 are the fields you defined in your Django model.



Import csv data into django models

How to write a Pandas Dataframe to Django model

Django bulk_create function example

Changing strings to Floats in an imported .csv