Django Project Layout

As a sort-of-followup to my brief remarks of last week, today I’m going to say a few words about how I lay out my Django projects, and how I configure the Apache server to present them. There’s nothing too surprising here, but since some of this is a little fiddly, I thought it worth going over.

Project Structure

I like to lay out an individual project (basically corresponding to a single “web site”) like this:

    static/		[directory for static media]
        adminmedia/	[symbolic link to django/contrib/admin/media/]
        imgs/		[just by way of example ...]
        django.wsgi	[core mod_wsgi/Django bridge]
    sharedapps/	[make sharedapps a python module]
        app1/		[shared app 1]
        app2/		[shared app 1]
        templates/	[shared templates]
            admin/	[shared overridden admin templates]
        app3/		[non-shared app 3]
        app4/		[non-shared app 4]

The sitespecific directory is the one you’d normally get when you run startproject, and the app3 and app4 directories are the ones that would normally be created by python startapp. Some of the other directories might take a word of explanation:

  • The static directory holds everything that I want to serve up w/o going through Django; Apache is configured to serve these files directly.
  • I set up the static/adminmedia symbolic link s.t. Apache can be configured to serve the admin media without referencing the Python installation directly.
  • The apache/django.wsgi script is what actually connects Apache (when set up for mod_wsgi scripting) to Django. I talk a bit more about it below.
  • The sharedapps directory holds those Django apps that have been decoupled from any particular Django project. It isn’t strictly necessary, but I prefer to keep the project-specific and decoupled apps separate. Note that the file, while empty, is necessary to make import statements work properly.


Here are some of the relevant parts of an exemplary Apache configuration script intended to be used with the project layout above:

# Single-thread, multi-process; Permissions may need to be set on the python-eggs directory
WSGIDaemonProcess django processes=10 threads=1 maximum-requests=10000 python-eggs="/Library/Webserver/.django-python-eggs"
WSGIProcessGroup django

# Alias the static media URL                                                                                       
Alias   /some/url/path/static			/some/filesystem/path/to/project/static

# Alias the application root URL to its script                                                                                
Alias   /some/url/path				/some/filesystem/path/to/project/apache/django.wsgi

# Enable serving of the static (including symlinked admin) media
<Directory "/some/filesystem/path/to/project/static/">
   Options FollowSymLinks			# To access admin media

   Order deny,allow
   Deny from all
   Allow from localhost				# ... or what permissions you prefer

# Enable WSGI scripting in the project's WSGI directory
<Directory "/some/filesystem/path/to/project/apache/">
   Options ExecCGI				# Set up the WSGI hander
   SetHandler wsgi-script

   WSGIPassAuthorization On			# As discussed last week

   Order deny,allow
   Deny from all
   Allow from localhost				# ... or what permissions you prefer

Note that I use Apache to serve static content; it’s actually recommended that you use another web server to do this when running Django. Such an arrangement is left as an exercise for the reader.


Here is the complete django.wsgi script:

import os
import sys

os.environ['DJANGO_SETTINGS_MODULE'] = 'sitespecific.settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

It doesn’t do anything too clever, aside from putting the parent of the sitespecific directory on the Python path. Note that all project-specific dotted-paths are relative to this directory; the sitespecific.settings syntax seen here is representative of what you’ll see in import statements throughout the code.

Because I’ve written my Django project under the assumption that the parent of the sitespecific directory is on the Python path, I have to tweak the script to support this assumption. Here’s the revised script:

#!/usr/bin/env python
from import execute_manager
    import settings # Assumed to be in the same directory.
except ImportError:
    import sys
    sys.stderr.write("Error: Can't find the file '' in the directory containing %r. It appears you've customized things.\nYou'll have to run, passing it your settings module.\n(If the file does indeed exist, it's causing an ImportError somehow.)\n" % __file__)

if __name__ == "__main__":
    # Hack s.t. the development server environment mirrors production
    import os
    import sys

    # Generated code

This arrangement will allow you to run the development server (python runserver) s.t. it will work in most cases. Be aware that most static media won’t be served up without some additional steps, not detailed here. (Admin media will work because of special hacks in the development server.) Also note that your application will always be presented at the root of the development server, instead of at the path you set in Apache configuration.

Share and Enjoy:
  • Twitter
  • Facebook
  • Digg
  • Reddit
  • HackerNews
  • Google Bookmarks
  • Slashdot
This entry was posted in Python, Web stuff. Bookmark the permalink.

One Response to Django Project Layout

  1. Pingback: Turbo OAuth for Django | Things that were not immediately obvious to me