Thursday, January 1, 2009

setting up django with mod_python

Important:

Before you do anything else, if you intend to use the admin feature in Django, make sure you go to your httpd.conf file and comment out this line:
LoadModule php5_module libexec/apache2/libphp5.so
For some reason mod_python.so and libphp5.so do not play nice together when it comes to the admin section of Django. If some kind person could either explain why or direct me to a site that has an explanation, that would be great.

Installing Django:

django can be found at - http://www.djangoproject.com/

I downloaded the svn version. Usually, I end up with several different versions. My django directory typically looks like this:

./
../
trunk_9600
trunk_9634
trunk_9650

I simply rename the trunks according to their version number and symlink ln -s ./django-trunk ./trunk_whatever This way, when I set up the symlink in the site-package directory to the django install, it is set up to the symlink django-trunk, which itself is linked to whichever trunk I happen to be using.

To link to the site-packages directory, do the following two commands:

  • SITE_PACKAGES_DIR=`python -c "from distutils.sysconfig import get_python_lib; print get_python_lib()"`
  • ln -s `pwd`/django-trunk/django $SITE_PACKAGES_DIR/django

Very good, now setting up apache.

Leopard is a bit quirky, but I want to use their way of doing things if at all possible. Here are some links to get around various problems:

kevin covers some basics on virtualhosting
aking discusses an important quirk to get multiple virtual host names working

I do my work on localhost, so my /etc/hosts file looks like:
127.0.0.1 localhost site1.dev something.whatever

then in my httpd-vhosts.conf file I have:

NameVirtualHost *

<VirtualHost 127.0.0.1 >
ServerName localhost
DocumentRoot "/Users/ronny/Sites"
</VirtualHost >

<VirtualHost * >
ServerName something.whatever
DocumentRoot "/Users/tickletik/Sites/mysite"

<Location "/" >
SetHandler mod_python
PythonHandler django.core.handlers.modpython
PythonPath "sys.path+['/Users/tickletik/Somewhere']"
SetEnv DJANGO_SETTINGS_MODULE mysite.settings
PythonDebug On
</Location >
</VirtualHost >

There are a few points to keep in mind
  • I'm assuming you are using the django tutorial, so I named it "mysite". mysite.settings is used with the DJANGO_SETTINGS_MODULE
  • if I wanted the DocumentRoot for something.whatever to be "/Users/tickletik/Sites" then I would have had to set Location to < Location "/mysite/" > DO NOT forget both slashes!

1 comment:

  1. There are conflicts between PHP and mod_python/mod_wsgi for various reasons. For some details search for PHP in:

    http://code.google.com/p/modwsgi/wiki/ApplicationIssues

    This is for mod_wsgi, which is probably a better choice than mod_python these days, but still relevant.

    ReplyDelete