Elvenware Logo



Learn about DJango.


Install pip:

sudo apt-get install python-pip

Install virtualenv:

sudo apt-get install python-virtualenv    

Create a virtual environment:

virtualenv test
cd test
source bin/activate

Install uwsgi:

sudo apt-get install python2.7-dev
pip install uwsgi

Start uwsgi:

uwsgi --http :8000 --wsgi-file

And ultimately:

uwsgi --http :8000 --module mysite.wsgi

A simple way to get started is to use pip:

sudo pip install django

This will probably install django into the following directory, or one similar to it:


An alternative install involves downloading the Django and then run the included setup program.

To download DJango go here: 


$ tar xzvf Django-1.3.1.tar.gz
$ cd Django-1.3.1/
$ sudo python install

python -c "from distutils.sysconfig import get_python_lib; print get_python_lib()"

In the following, I run a test to see if Django is installed. The error shown below after the attempt to import django indicates that Django is not installed:

charlie@WesternSea:~/Downloads/Django-1.3.1$ python
Python 2.7.2+ (default, Oct  4 2011, 20:03:08)
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> pring django.get_version()
  File "<stdin>", line 1
    pring django.get_version()
SyntaxError: invalid syntax
>>> print django.get_version()

Here is how it looks if Django is installed:

charlie@WesternSeas:~$ python
Python 2.7.3 (default, Aug 1 2012, 05:16:07)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django

As you can see, when everything is set up properly, you can type import django at the Python prompt without getting an error. (If you get an error when you simply type the word python at the command prompt, that means that Python is not installed. I cover installing Python here.

To learn more about DJango, go here:

Intro Tutorial

Set up a web site

First create a site: startproject mysite

You should now navigate to the mysite folder and spend some time exploring it.

mkdir mysite
cd mysite
python runserver
Validating models...

0 errors found
Django version 1.3.1, using settings 'mysite.settings'
Development server is running at
Quit the server with CONTROL-C.
[29/Jan/2012 00:58:21] "GET / HTTP/1.1" 200 2051

Okay, your site is set up, and a toy webserver is running.

Go to http://localhost:8000/

Django start screen

Running Under Apache with WSGI

The example above shows how to get django running under a test server. When you deploy, and sometimes even during development, it is best to run under Apache. Fortunately, it is not difficult to run django under Apache. To begin, install a program called WSGI.

To install mod-wsgi under Ubuntu:

sudo apt-get install libapache2-mod-wsgi

Now you need to edit your configuration file Apache, which is in the sites-available directory:

sudo gedit /etc/apache2/sites-available/default

Edit the code so it looks like this:

<Directory /var/www/>
    Options Indexes FollowSymLinks MultiViews ExecCGI

    AddHandler cgi-script .cgi
    AddHandler wsgi-script .wsgi

    AllowOverride None
    Order allow,deny
    allow from all

You can then restart apache like this:

sudo /etc/init.d/apache2 restart

Now place a hello world application called index.wsgi in /var/www:

def application(environ, start_response):
    start_response('200 OK', [('Content-Type', 'text/plain')])
    yield 'Hello World\n'

You can also make index.wsgi a default page by editing dir.conf:

sudo gedit /etc/apache2/mods-enabled/dir.conf

After opening the file, add index.wsgi to the list of recognized default files.

At this stage, you have wsgi up and running. Your next step is to link in Django, as explained here:

To add in an existing Django project, just add code like the following to the bottom of your sites-available/default file, and outside the virtualhost section:

WSGIScriptAlias / /home/charlie/django/myproject/myproject/
WSGIPythonPath /home/charlie/django/myproject

<Directory /home/charlie/django/myproject/myproject>
        Order deny,allow
        Allow from all

Then restart apache.

Static Files

The simplest way to serve up static files is to create an app, put a static directory in it, and then put your static files in that static directory. Assume you have project called myproject. Inside it you create an app called called books:


In order to get CSS for your admin working correctly, you might also need to add the following below WSGIPythonPath:

Alias /static/ /usr/local/lib/python2.7/dist-packages/django/contrib/admin/static/

The whole thing would then look like this:

WSGIScriptAlias / /home/charlie/django/myproject/myproject/
WSGIPythonPath /home/charlie/django/myproject
Alias /static/ /usr/local/lib/python2.7/dist-packages/django/contrib/admin/static/

<Directory /home/charlie/django/myproject/myproject>
        Order deny,allow
        Allow from all

If you want to start serving up your own static pages, then do something like this where you point to a directory on your own site:

Alias /static/ /home/charlie/django/myproject/myproject/static/

In, there should be no need, at least at first, to change these settings:


# URL prefix for static files.
# Example: ""
STATIC_URL = '/static/'

For more information, go here:


Shell Prompt

If you get an error about DJANGO_SETTINGS_MODULE is undefined when working at the Python prompt, then start Python like this:

python shell

This is just the same as typing pythonbut now your file has been run and your paths and other settings are properly configured.


When modifying a sqlite3 database, both the database itself, and the folder it is in, should be writable by Apache:

sudo chown charlie:www-data mydatabase.db
chmod 664 mydatabase.db
sudo chown charlie:www-data myDataFolder
chmod 775 myDataFolder

Remember you can always check the current values for file or folder with this command:

stat -c '%A %a %n' *  

Here is code for installing mysql:

sudo apt-get install python-mysqldb
python syncdb
charlie@WesternSea:~/Source/mysite$ python sql polls
CREATE TABLE `polls_poll` (
    `question` varchar(200) NOT NULL,
    `pub_date` datetime NOT NULL
CREATE TABLE `polls_choice` (
    `poll_id` integer NOT NULL,
    `choice` varchar(200) NOT NULL,
    `votes` integer NOT NULL
ALTER TABLE `polls_choice` ADD CONSTRAINT `poll_id_refs_id_5d896c23` FOREIGN KEY (`poll_id`) REFERENCES `polls_poll` (`id`);
charlie@WesternSea:~/Source/mysite$ python syncdb
Creating tables ...
Creating table polls_poll
Creating table polls_choice
Installing custom SQL ...
Installing indexes ...
No fixtures found.
charlie@WesternSea:~/Source/mysite$ python shell
Python 2.7.2+ (default, Oct  4 2011, 20:03:08)
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from polls.models import Poll, Choice
>>> Poll.objects.all()
>>> import datetime
>>> p = Poll(question="What's up?",
>>> p.question
"What's up?"
>>> p.pub_date
datetime.datetime(2012, 1, 29, 1, 57, 57, 496429)
>>> Poll.objects.all()