Evaluating Django

Posted by – September 1, 2008

In the next weeks I’ll try to take a closer look on this framework, it has a lot of nice features, one of then is your O/R mapper, very easy to work with, you can create your database using the model classes by running few commands, giving an idea about how much productive this framework can be for database driven applications.

Here’s a example of django model classes:

models.py

1
2
3
4
5
6
from django.db import models

class Friend(models.Model):
    name = models.CharField(max_length=200)
    phone_numer = models.CharField(max_length=200)
    birth_date = models.DateTimeField('birth date')

You can generate the application database containing “friend” table by just running the command python manage.py sql appname and python manage.py syncdb.

But the model layer isn’t enougth on a web application framework, we still still need consider the controller and view layers.

Django allows to define a method that can be used to handle a request on a specific view of our application. These methods are defined on myapp/views.py script.

views.py

1
2
3
4
5
6
7
8
9
10
from myproject.myapp.models import Friend
from django.http import HttpResponse

def index(request):
    friends_list = Friend.objects.all().order_by('-birth_date')[:5]
    t = loader.get_template('myapp/index.html')
    c = Context({
        'friends_list': friends_list,
    })
    return HttpResponse(t.render(c))

When the index method is called, we retrieve a list of Friend object instances and add this list as context entry, the context contains several entries, where each entry defines a mapping of template variable to python object, once a context is set, we will render our template using this context data and then generate a response to the client.

In order to call this method properly, we must define some url patterns, these url patterns contains uri to view method mapping, all of then defined on myapp/urls.py script like we have in the example below:

1
2
3
4
5
from django.conf.urls.defaults import *

urlpatterns = patterns('',
    (r'^myapp/$', 'myproject.myapp.views.index'),
)

With the url pattern configuration defined, the view templates must be created, Django templating system allows templates to be extended, you can create a base template that defines the layout and then use child templates to add content on your page.

The example below contains an example of django view template that defines the layout our pages:

layout.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <link rel="stylesheet" href="style.css" />
    <title>{% block title %}My amazing site{% endblock %}</title>
</head>

<body>
    <div id="sidebar">
        {% block sidebar %}
        <ul>
            <li><a href="/">Home</a></li>
            <li><a href="/new/">Add Friend</a></li>
        </ul>
        {% endblock %}
    </div>

    <div id="content">
        {% block content %}{% endblock %}
    </div>
</body>
</html>

You can extend your template by creating the file below:

index.html

1
2
3
4
5
6
7
8
9
10
11
{% extends "layout.html" %}

{% block title %}My amazing friends list{% endblock %}

{% block content %}
{% for friend in friends_list %}
    <h2>{{ friend.name }}</h2>
    <p>{{ friend.birth_date }}</p>
    <p>{{ friend.phone_number }}</p>
{% endfor %}
{% endblock %}

The template above does a loop over friends_list variable, this variable has been set in the index method of view.py, the python object containing a list o Friend object instances has been passed at rendering time. You can see the output of template above by loading the following url in your browser:

http://localhost/myapps/

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>