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 -> urls.py -> views.py -> models.py -> 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:


Project_Name
    templates
        app1
             index.html
        app2
             index.html

    App1

        models.py

        views.py

        urls.py

    App2

        models.py

        views.py

        urls.py

    settings.py

    urls.py

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

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

#urls.py
from django.conf.urls import patterns, url
from blog.news import views

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

That URL Pattern will dispatch HTTP Requests and reply with HTTP Responses from views.news() 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 views.py module? For example, here it is:

from blog.news.models 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({
       'news':news,
    })
    return render_to_response('templates/news/view.html', context, context_instance = RequestContext(request))

You can see that views.py get data from blog.news.models module a.k.a models.py file, right beside views.py. This is how models.py 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 models.py 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 🙂

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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