Peter's Blog

Redefining the Impossible

Django mod_python and static file serving


My embroyo Django blog got to the stage where I wanted to theme it. I started off by giving it the theme from this site which requires serving of various graphic files as well as the .css file. This means setting apache up for serving static files as well as dynamic pages. Django can serve static files but it is better to use apache as that is what apache is good at. The django advise is to use a differrent server for the static files, something lean and mean, but I'd prefer to use the one server. I get about 500 page loads/day, performance is not totally paramount to me.

I achieved this using the following apache virtualhost setup (in /etc/apache2/sites-available/mysite for debian and apache2).

<VirtualHost *>
    ServerName sitename.org
    ServerAlias sitename.org *.sitename.org
    ServerAdmin webmaster@localhost

    DocumentRoot /var/www/sitename.org
    <Directory />
        Options -Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all
    </Directory>

    #
    # Most stuff is handled by django
    #
    <Location "/">
        SetHandler python-program
        PythonHandler django.core.handlers.modpython
        PythonPath "['/usr/local/lib/Django'] + sys.path"
        SetEnv DJANGO_SETTINGS_MODULE django_local.settings
    </Location>

    #
    # Theme files are static and not done by django
    #
    <Location "/theme">
        SetHandler none
    </Location>

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    ErrorLog /var/log/apache2/sitename.org/error.log

    CustomLog /var/log/apache2/sitename.org/access.log combined
    ServerSignature On

</VirtualHost>

Here I use the Location keyword to tell mod_python to handle all accesses to the site. Then I override this for the subdirectory /theme where I set the handler to none, causing static content to be served.


Filed under: apache django python

Have Your Say

I welcome constructive comments or questions but I reserve the right to delete any comments that displease me.

Who are you?

(Optional) If you enter an email address here I might email you back. Your email address will not be sold to spammers or shown anywhere

What do you have to say?