How Django Works

We already know that Django is a web framework. But now, how it works? It’s useless if you randomly develop a Django application without understanding about its concepts. The first question is: Where should I start?

To answer that question, let’s take a look about a Django application’s request flow:

web browser address bar -> -> -> -> database

As stated on Django documentation, a Django project consists of applications. Each of application has Python modules. Each Python module has classes or functions. The structure is like this:




You can place the templates folder outside or inside the apps, depending on your TEMPLATE_DIRS on

So, the answer is, start with, and then,,, and the last is templates. Django will first check file to verify that your Django project is configured properly. After that, Django will dispatch request on That contains URL Patterns that constructed in regex (Regular Expression). For example:
from django.conf.urls import patterns, url
from import views

urlpatterns = patterns('',
    url(r'^news/(?P<id_news>\d+)$',, name='news'),

That URL Pattern will dispatch HTTP Requests and reply with HTTP Responses from function.  The function will return News with id = id_news, and then render a template to display data. For example, if you used localhost:8000 as your host & port, url http://localhost:8000/news/3 will display a page containing a news with id = 3.

What is inside that module? For example, here it is:

from import News
from django.shortcuts import render_to_response
from django.template.context import RequestContext, Context

def news(request, id_news)
    news = News.objects.get(id_news = int(id_news))
    context = Context({
    return render_to_response('templates/news/view.html', context, context_instance = RequestContext(request))

You can see that get data from module a.k.a file, right beside This is how file looks like:

from django.db import models

# Create your models here.

class News(models.Model):
    id_news = models.AutoField(primary_key = True)
    text = models.TextField()
    timestamp = models.DateTimeField()

    class Meta:
        db_table = 'news'

That file of course, get data from database via Django’s built-in ORM (Object-Relational Mapping). You can define the table name with Meta class, and the column names are exactly the same as News class properties.

Pretty simple, right? Those are just basic concepts, I will explain concepts of templating later. Once you know about these basic concepts, getting started with Django will be easy. Hope this helps 🙂


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s