Contact me:
Contact me

My First Django Application

I am involved in a new project, which is implemented using django framework.

Here I present a simple django application. You can download here .

I strongly recommend a first quick reading of The django Book . There you can get a spread explanation of the code I present here.

Preparing the framework.

There are you ways to start up.

  • You can download django and install it in you operating system.
  • download the source and set enviroment variables

I prefer the last one because the presented code here is not working for sure with the new django realease. One of the problems that django has is that it doesn't present backward compatibility so sometimes it a little tough to upgrade django version in a project.

I the tar file . you can find the django source code used.

First, set the enviroment variable to the django source (using linux os):

export PYTHONPATH=/home/mcm/projects/django/beni/django/src/

Checking django version

This way, when you call, for example,, you are using the django sources of the exported path, in this case a non official realease between 0.96 version and 1.02:

mcm@McM:~$ help [options] [args]
Django command line tool, version 0.97-pre-SVN-unknown
Type ' help ' for help on a specific subcommand.

Or you can type in the python console as follows:

mcm@McM:~$ python
Python 2.5.2 (r252:60911, Oct 5 2008, 19:24:49)
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> django
>>> django.VERSION
(0, 97, 'pre')

Starting the application

Now, you can start your new project following the django book guide reference :

mcm@McM:~$ python startproject mysite

To start the application, we can use the django server just typing:

$ python runserver

Django version 0.97-pre-SVN-unknown, using settings 'miki.settings'
Development server is running at
Quit the server with CONTROL-C.


The dummy application has the following urls:

Running the script


Configuring the database

mcm@McM:~$ mysql -u root -pEnter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 321
Server version: 5.0.67-0ubuntu6-log (Ubuntu)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> create database djangotest;
Query OK, 1 row affected (0.03 sec)

mysql> create user django identified by 'adm';
Query OK, 0 rows affected (0.16 sec)

mysql> GRANT ALL ON djangotest.* TO 'django'@'localhost' IDENTIFIED BY 'adm';
Query OK, 0 rows affected (0.00 sec)

Syncronizing models with database

Once you have configured the file with the database name, user and password configuration, you must syncronize the models with the database:


    DATABASE_ENGINE = 'mysql' # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
    DATABASE_NAME = 'djangotest' # Or path to database file if using sqlite3.
    DATABASE_USER = 'django' # Not used with sqlite3.
    DATABASE_PASSWORD = 'adm' # Not used with sqlite3.
    DATABASE_HOST = '' # Set to empty string for localhost. Not used with sqlite3.
    DATABASE_PORT = '' # Set to empty string for default. Not used with sqlite3.

mcm@McM:/tmp/miki$ python syncdb

Creating table auth_message
Creating table auth_group
Creating table auth_user
Creating table auth_permission
Creating table django_content_type
Creating table django_session
Creating table django_site
Creating table django_admin_log
Creating table books_publisher
Creating table books_book
Creating table books_author

You just installed Django's auth system, which means you don't have any superusers defined.
Would you like to create one now? (yes/no): yes
Username (Leave blank to use 'mcm'):
E-mail address:
Password (again):
Superuser created successfully.
Installing index for auth.Message model
Installing index for auth.Permission model
Installing index for admin.LogEntry model
Installing index for books.Book model

Database initial state

  • Every model will have a linked table
  • Django will create other tables when the model has manytomany parameters, in this case the Book model: authors = models.ManyToManyField(Author)

In this case, 4 tables linked to the models are created:

  • | books_author |
  • | books_book |
  • | books_book_authors |
  • | books_publisher |

The other tables are automatically created by django framework:

mysql> show tables;
| Tables_in_djangotest |
| auth_group |
| auth_group_permissions |
| auth_message |
| auth_permission |
| auth_user |
| auth_user_groups |
| auth_user_user_permissions |
| books_author |
| books_book |
| books_book_authors |
| books_publisher |
| django_admin_log |
| django_content_type |
| django_session |
| django_site |
15 rows in set (0.00 sec)

Share it!

Twitter Gmail Delicious Google Bookmarks Hotmail Yahoo Mail Share/Bookmark