giovedì 5 aprile 2012

Django 1.4 for Ubuntu 12.04

Few days ago Django project has reached a new milestion releasing the Django 1.4.
Main improvements covered:
- Better support for time zones;
- Support for in-browser testing frameworks;
- Updated default project layout and;
- Custom project and app templates;
- Improved WSGI suppor;
- Improved password hashing;
- HTML5 doctype;
- List filters in admin interface;
- Multiple sort in admin interface;
- New ModelAdmin methods;
- Admin inlines respect user permission;
- Tools for cryptographic signing;
- Cookie-based session backend;

So I decided to try it on my brand new Ubuntu 12.04 server machine.
Django 1.4 requires python 2.5 but Ubuntu comes with python 2.7, so this requirement is satisfied.

1. Install the server

Unfortunately on Ubuntu 12.04 Django 1.4 is not yet packaged so we need to install it manually:

 wget "" -O Django-1.4.tar.gz
 tar xzvf Django-1.4.tar.gz
 cd Django-1.4
 sudo python install

After that you can install your preferred web server:

 apt-get install apache2
 apt-get install libapach2-mod-wsgi

2. Test if Django is working

Ok now let’s configure your 1st Django site. We assume your site will be called "HelloDjango".

 cd /var/www/ startproject HelloDjango

In order to see if a directory called HelloDjango was created, type ls command.

 cd HelloDjango

Now we create a wsgi file for the site typing:

mkdir apache
vi ./apache/django.wsgi

Add to the file this content:

import os
import sys
path = '/var/www'
if path not in sys.path:
    sys.path.append(0, path)
os.environ['DJANGO_SETTINGS_MODULE'] = 'HelloDjango.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

Configure Apache

Now it's time to create a brand new Virual Host configuration. So create a file called HelloDjango

vi /etc/apache2/sites-available/HelloDjango

Put this lines to that file

<VirtualHost *:80>
    DocumentRoot /var/www/wsgi
    <Directory /var/www/wsgi>
        Order allow,deny
        Allow from all
    WSGIScriptAlias / /var/www/HelloDjango/apache/django.wsgi

Activate the site and restart Apache

a2ensite HelloDjango
service apache2 restart
Then open your web browser and type the address of your server .. you should see the Django default installation message.

That's All and now enjoy the Django web framework on your Ubuntu system.
Posta un commento