Tango with Django


Django is a web framework, written purely in Python language. It helps programmers to build complex web applications quickly. The name Django comes from the early Jazz guitarist Django Reinhardt. He was a gypsy whose two fingers on his left hand were paralyzed in an accident when he was young. And, that the inspiration which Django puts in the framework as well, as it can do complex things with less amount of code and with simplest of execution than you would expect. You do not need to be an expert in Python to get started with Django as well.


Django was developed over a period of two years by programmers who were working for an online publishing company. The name of the company is Lawrence Journal-World Online, in Lawrence, Kansas. When the programmers were done, they realized they have a full stack web framework in there hands, and released it to public under open-source BSD licence. From there on it became a community project, and people started using it in different projects. There is also a list of Django powered sites, check out Django Sites.

Django is much often perceived as web content management system, which it is not, its a web framework. It has a Django CMS separately, and could work as a CMS if you build it like that.

Django has derived slightly different pattern called MTV, which means model, template, view. This might be confusing for people at the first glance who are familiar with MVC, i.e; model, view, controller. It is the same thing in Django, just the way its referred is different.

DRY Principle

One of the common developer challenges is to handle purposeful duplication. Duplication leads to maintenance nightmares, poor factoring, and logical contradictions. Duplication, can arise anywhere: in architecture, requirements, code, or documentation, but in Django we focus on the code level duplication. Django has a strict adherence to Dont Repeat Yourself (DRY) principle, i.e; Why write the same code over and over again?

The beauty of Django is that you will find it very fast, nimble and has the capability of generating a prototype website in a very short amount of time. Django slogan says it all: “The web framework for perfectionists with deadlines.”

Architecture overview

Imagine you have a simple wireframe and basic knowledge of Python programming, how long do you think will take you to build a working demo site? With Django in your kit, its not going to take even half a day! Yes, that easy Django is.

Django is self-contained, it has a command line interface, an inbuilt development server, an inbuilt admin interface, what else do anyone need to get up and start?

The inbuilt development server in Django is not for production level, it should only be used with local environment for testing and development purposes. The preferred way to run Django on production is through mod_python or mod_wsgi.

Now, lets have a brief look at starting a project with Django as it is lot easier than explaining how to build it.

Here is what you need to do create a project in Django through command line:

$ django-admin startproject myproject

The above line will generate a project folder structure with all the essential files needed for a project to run. It consists of base settings file, which could help you set database connections, template locations and you can also put you site wide constants there. In the same folder you will find urls.py which is the main url config file for defining the mappings of urls. The folder also has manage.py which contains a standard set of command line tools.

Now, after creating the project you can get started to create Django apps, or basically reusable pluggable python modules, but in Django style. For that you have to run the following command inside the myproject through command line:

$ cd myproject

$ python manage.py startapp firstapp

This will generate an app folder structure, which consists of files like, urls.py, models.py and views.py, and also a folder called migrations, which handles database migrations, based on models inside models.py. The file views.py is used to define views or in a way you can say, it contains controller functions in context of MVC pattern.

Django has its own template language and as with many features of Django, using the inbuilt template language, is totally optional. You can use any other template language engine and configure it in settings.py file.

The other thing to remember is Django requires Python 2.3 or higher. Mac OS X and most of the Linux distros ship with Python 2.5, so, its not a problem for those who have these machines.

How does Django works?

The simplest way to look at Django is to break all its related components. Lets have a look at models.py, it is a simple Python file, which defines the data model by extrapolating single lines of code into database tables. And you can create a CRUD section of your model in Django admin, which is another rich feature of Django, by registering it using admin.py.

The urls.py, as said earlier is used to map the urls, may be in a simple way or by using regular expressions.

Then comes the meat of Django, it is the views.py, it process the data from models and then pass it to templates to show the data to the user.

Step by step as it happens when you land on a Django page:

  1. Django goes through all the url patterns and uses the information to get to the view the url config is pointing to.
  2. The view then processes everything, if model has to be included to fetch the data, it will do it, and then do your business logic and put it all in the template.
  3. The view passes the requested information to the template it relates to.
  4. The template then renders the data in a layout and then displays the page to the user.

So, as you could see, Django is a nice, easy, robust web framework. I will soon put on some tutorials for you to tango with Django!