Apache with Django and mod_wsgi

, 122 words

Most of the development work I'm doing at the moment is in Django, and I've recently switched to using mod_wsgi. In putting together our Apache and wsgi configuration, I found that neither mod_wsgi's nor Django's wsgi installation instructions contained a truly portable configuration include. I came up with the script below. It's far from perfect but it does the job for now:

# Assumes this is in the project root, parallel to settings.py, etc.
# We'd typically have our 'docroot' folder one level down for security reasons.
import os, sys, django.core.handlers.wsgi

project = os.path.dirname(__file__)
workspace = os.path.dirname( project )

sys.path.append( project )
sys.path.append( workspace )

os.environ['DJANGO_SETTINGS_MODULE'] = '%s.settings' % project.rpartition( '/' )[2]
application = django.core.handlers.wsgi.WSGIHandler()

Happy hacking!