Deploy Django on a subdirectory (or a subpath) with mod_python

It doesn’t matter for how long you’ve been using Django – as much as you think you know it, at some point Django will surprise you transforming simple tasks into issues taking hours to be solved. I’ve never had the need to deploy a Django instance anywhere but the root of the domain name (e.g. http://example.com/myapp). Till last weekend, when I spent almost two hours to figure out how to do it without making a mess of the urls.py module (configuring Apache and mod_wsgi to serve a subpath was straightforward and it’s not covered by this article).

Actually Django does a really good job handling URL prefixes and, to be honest, I wouldn’t write these lines if I didn’t make use of the bittersweet Django Admin. AFAIK, problems arise in the following:

  • Your webserver does not set SCRIPT_NAME environment variable
  • Using the django-admin
  • Using {% url %} template tag
  • Using @login_required decorator

For the latter there’s a quick solution: set LOGIN_URL and LOGOUT_URL variables in your settings.py with the desired url path. In all the other cases, you’ve to play with the SCRIPT_NAME environment variable,  defined in CGI standard, which holds the request URL’s path which points at the application target. Well hidden (IMHO) at the bottom of this page you can find a quick and clean solution to manually set the SCRIPT_NAME variable: just define FORCE_SCRIPT_NAME in your settings.py, e.g. FORCE_SCRIPT_NAME=’/myapp’ without the trailing slash. That’s all.

Milliways, the game at the end of the Universe

Milliways is a game developed for the “5 BUTTONS Competition” (follow the link for the details) contest hosted at experimentalgameplay in collaboration with 02L. Games have to support UnitaZero as their one and only input device, and that’s the fun part!

Basically it’s a cooperative puzzle game (2-5 players) in Tetris style: a row of 5 dice fall to the bottom of the screen and each pad change the face of the corrisponding dice when triggered. When all the 5 dice shows the same face the row disappear. If the rows reach the top of the screen the game is over (you know how Tetris works, don’t you?). Game concept and development are very simple but players’ interaction could be interesting (or I hope so).

Tracks played during the game are courtesy of  The Shaidon Effect, the rest of the components is in the public domain or has a CC license.

The game was developed using Python and Pygame, the code is open source and you can find it here: https://bitbucket.org/masci/milliways/

I wrapped-up a Windows executable with PyInstaller, tested only on a Win7 Pro SP1 machine: http://dl.dropbox.com/u/6417369/milliways.zip

The game is named after “The restaurant at the end of the universe” –  I was reading that novel during the early development.