Most django sites I create have quite a lot in common. Beyond the handful of files generated by django-admin startproject, my projects all have a database, wsgi file, apache and nginx confs, static media and templates. All these building blocks of a site vary very little from project-to-project. Take for example a typical apache virtualhost:
<VirtualHost *:8000>
ServerAdmin webmaster@example.com
ServerName example.com
ServerAlias www.example.com
<Directory /home/example/public_html/example.com/example/apache/>
Order deny,allow
Allow from all
</Directory>
LogLevel warn
ErrorLog /home/example/public_html/example.com/logs/apache_error.log
CustomLog /home/example/public_html/example.com/logs/apache_access.log combined
WSGIDaemonProcess example.com user=www-data group=www-data threads=25
WSGIProcessGroup example.com
WSGIScriptAlias / /home/example/public_html/example.com/example/apache/example.wsgi
</VirtualHost>
Much of the information in this file is repetitive and follows a fairly obvious naming scheme. The same goes for lots of the other basic pieces of Django sites. By using Django's own template language, I've written a handy utility for generating all the basic files I need to get a django project off the ground quickly. The sysadmin at my job has been doing this for several years to generate confs, so props to him for the great idea!
How it works
A configuration file specifies all the basic information that does not change, like your username, pythonpath, the base directory all sites live in, etc. It also specifies patterns to use for laying out directories for each project. My personal setup (and the one that is configured by default) is based heavily on these two great tutorials:
- http://www.meppum.com/2009/jan/17/installing-django-ubuntu-intrepid/
- http://lethain.com/entry/2009/feb/13/the-django-and-ubuntu-intrepid-almanac/
Basic setup
The script assumes a basic setup of using nginx and apache to serve django websites. Nginx serves static media and proxies all other requests to Apache running mod_wsgi.
- ~/public_html/site_domain.com/site_domain/ < django app lives here >
- nginx serving static media
- apache serving dynamic content with mod_wsgi
- postgres database server
Note: this script is highly configurable and can easily be altered to work with your setup - this is just the default.
What happens when you run it?
By default it doesn't mess with anything outside of the base site directory, so you don't have to worry! Here is some sample output from running it on my laptop to create a site named "testing" that will have apache listening on port 9000:
~/bin/django-site-gen$ ./generate_site.py testing 9000
Writing /home/charles/public_html/testing.com/testing/manage.py
Writing /home/charles/public_html/testing.com/testing/settings.py
Writing /home/charles/public_html/testing.com/testing/urls.py
Writing /home/charles/public_html/testing.com/testing/__init__.py
Writing /home/charles/public_html/testing.com/testing/apache/testing.wsgi
Writing /home/charles/public_html/testing.com/logs/nginx_access.log
Writing /home/charles/public_html/testing.com/logs/apache_access.log
Writing /home/charles/public_html/testing.com/logs/apache_error.log
Writing /home/charles/public_html/testing.com/logs/nginx_error.log
Writing /home/charles/public_html/testing.com/apache_testing.com
Writing /home/charles/public_html/testing.com/nginx_testing.com
Writing /home/charles/public_html/testing.com/ports.conf
Writing /home/charles/public_html/testing.com/hosts
Writing /home/charles/public_html/testing.com/local_apache_testing.com
Writing /home/charles/public_html/testing.com/local_nginx_testing.com
Writing /home/charles/public_html/testing.com/local_hosts
Creating database testing_main
Copying media directory
Copying template directory
Done!
Those first files should look familiar - they are the ones generated by django for you when you create a new project, but without some of the settings cruft. Log files are created for apache and nginx, and two sets of confs are generated for the new site: apache_testing.com/nginx_testing.com and local_ versions. The local_ versions, as you might guess, are for local development and the only difference between them and the production versions is they refer to testing.site instead of testing.com. There are, additionally, stub files that can be appended to /etc/hosts and /etc/apache2/ports.conf.
This project is still a bit rough around the edges but it's been working for me and could probably work for you! Check it out if you're interested, and please fork liberally - it's on github:
Further reading
- http://cloudsilverlining.org/django-quickstart.html
- http://github.com/SmileyChris/django-startproject
Comments (2)
Commenting has been disabled for this entry

tomvons
I think it's a little crazy that there is no "standard" Django site layout. I've worked with a few different developers and seen quite a few different projects, and nobody really does it the same way.
Thanks for sharing yours, though.
May 3, 2010 at 10:36 p.m. ( permalink )
Charles Leifer
Actually, there are some standards out there for django projects beyond pep8, although I just recently learned about them. Check out http://lincolnloop.com/django-best-pr... -- some great stuff there.
May 3, 2010 at 11:01 p.m. ( permalink )