Daily Archives: July 30th, 2008

In the previous entry I explained how to create a new project, app and how to add an app to the project. In this entry, I’ll show how to implement a MVC in Django.

Recall that our project directory tree looks as follows:

<project_name>/

<app_name>/
__init__.py
models.py
views.py

__init__.py
manage.py
settings.py
urls.py

The first thing we need to do is to tell Django which methods will handle requests. This is done in the urls.py by adding a tuple of tuples with a name urlpatterns. The tuples are of the form:

(regular expression, Python callback function [, optional dictionary])

The first item is a regular expression that matches URL we want to handle. The second item is the full path to the function to handle the request. The dictionary is keyword arguments. The methods to handle the request must be inside the views.py file.

Inside the handler methods we can do anything that Python allows us to do. One condition is that the method must return an object of type HttpResponse. The next step is to create templates to handle the presentation. First thing we must do is decide where to put templates. The path to templates is added to TEMPLATE_DIRS tuple in settings.py. After we’ve added this template, we must tie it with the method that handles the request. This is done by creating a django.template.Context object, which is just a mapping of Python objects inside the method and their respective names inside the template. Then, we must load the actual template by calling django.template.loader.get_template(<template_name>). Finally, we render the template by calling render on the loader and passing to it the Context object, and return the resulting HttpResponse object.

Is really, really good. It’s a MVC web framework written in Python with content-management in mind. The most general unit in Django is a project, which is a collection of applications. Applications are just plugins, and can be used in one or more projects. Remember my previous post on adding a new component to DrProject? Well, with Django it’s a lot simpler, as I’ll show soon. Django supports PostgreSQL, MySQL, Sqlite 3, and Oracle. It also comes with database API, which is very similar to SQLAlchemy.

To create a new project, we can use django-admin.py to create directory structure so we don’t have to do it manually. When we invoke django-admin.py startproject <project_name>, Django will create the following directory tree:

<project_name>/
__init__.py
manage.py
settings.py
urls.py

For example, the settings.py file just contains setting fields (such as database-related stuff). The backend follows the DRY (Don’t Repeat Yourself) principle. This means that we define anything that we want to store in the database as a model. A model is essentially a class inheriting django.db.models.Model with fields, a concept similar to Elixir/SQLAlchemy. A model belongs to an application that uses that model. To create a new app in Django, we go to the directory in which the project was created and invoke python manage.py startapp <app_name>. This creates the following directory tree:

<app_name>/
__init__.py
models.py
views.py

This looks like MVC, right? In models.py we specify the models that we’ll want to store in the database. When we’ve added models to this file, we call python manage.py syncdb from the project root directory, which calls execute_manager from django.core.management with our settings, and applications. One little caviat – we need to manually add our application to INSTALLED_APPS tuple in the settings.py using its full name (<project_name>.<application_name>). When this command is called, Django will create any tables related to our models.

This completes the intro to creating projects/apps and defining models. Next, I’ll show how to add controllers to handle requests and templates to show shiny stuff.