Django in Ubuntu

As I use Django in Ubuntu, I would like to point out little quirks here.

Install Django

Installing Django in Ubuntu is quite simple:

sudo apt-get install python-django

Django is installed in the path /var/lib/python-support/python2.5/django

Since I will be using MySQL, I needed to install another package which would let Python connect to it.

sudo apt-get install python-mysqldb

Start New Project

While trying to create a new project, run the following command:

cd /home/me/

django-admin startproject newprojectname

Although the documentation says to use, in Ubuntu Gutsy, you should not use the .py extension. Instead, just use django-admin. The source of this information is a post on Ubuntu Forums: Path to Django Installation.

Start a New Application

Go into the directory of your project. For example, if your project is called newprojectname, then

cd /home/me/newprojectname

Then run the following command

python startapp newappname

Inspect the Database

If you need to generate a model of your database automatically, you can easily inspect the database by first going into the directory of the project

cd /home/me/newprojectname

and then running this

python inspectdb

I usually save the output to a file so that I can look at it and make changes to it as required.

python inspectdb > ~/

In my example, can be used as a file after rearranging the contents (Cleaning Up Generated Models).

Deploy Django with Apache

Although the Django Book has a very good way of explaining this stuff (Using Django with Apache and mod_python), I would like to explain the same thing step-by-step.

Install Apache: sudo apt-get install apache2

Install mod_python: sudo apt-get install libapache2-mod-python

Check if mod_python is enabled: ls /etc/apache2/mods-enabled/mod_python.load and if you see mod_python.load as a result, it is enabled

If mod-python is not enabled: sudo a2enmod mod_python

Restart Apache server: /etc/init.d/apache2 restart

Configure Virtual Host

Your virtual host config file should look something like this

sudo vim /etc/apache2/sites-available/mysite

NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot /home/me/mysite/
ErrorLog /home/me/logs/error.log
<Location "/">
SetHandler mod_python
PythonHandler django.core.handlers.modpython
PythonPath "['/home/me/'] + sys.path"
SetEnv DJANGO_SETTINGS_MODULE mysite.settings
PythonDebug On

Of course, if it is a production site, PythonDebug On should be omitted.

If you are moving your Django site from one server to another, it might be a good idea to first create a new site on the new server with the same name as the site being transferred. Then just copy all the files and folders into the new site. I don’t know how Django works in this regard but it is possible it keeps a list of projects created.

How Do I do it?

I have created a directory for templates and static (css, images, etc.) files: /home/me/templates/ and /home/me/templates/static/. My Django application resides in /home/me/mysite/. My file looks like

DocumentRoot /home/me/templates/
ErrorLog /home/me/logs/error.log
LogFormat "%v %l %u %t \"%r\" %>s %b" comonvhost
CustomLog /home/me/logs/custom.log comonvhost
<Directory /home/me/templates/*>
Options -Indexes
<Location "/">
SetHandler mod_python
PythonHandler django.core.handlers.modpython
PythonPath "['/home/me/'] + sys.path"
SetEnv DJANGO_SETTINGS_MODULE myapp.settings
PythonDebug On
<Location "/static">
SetHandler None
<Location "/media">
SetHandler None

I also created a symlink to the Django admin media files in /home/me/templates/ (my apache root directory) so that would look the way it’s supposed to with all its GUI intact.

cd /home/me/templates/
ln -s /var/lib/python-support/python2.5/django/contrib/admin/media media

Hat tips to: Django Installation on Apache; Serving the Admin Files;

2 Responses to Django in Ubuntu

  1. sudo a2enmod mod_python –> this is out of date.
    for me it was :
    apt-get install libapache2-mod-wsgi
    apt-get install libapache2-mod-python

    sudo a2enmod python
    sudo a2enmod wsgi

  2. Arlando says:

    This may be wrong. I found my django distribution at:


%d bloggers like this: