Multiple websites in the same Django project

Want to run two websites from the same Django project? It took me a white to find this one out so I thought I’d post it up here.
Basically you need a couple of steps… First add django.contrib.sites to the installed apps in your settings.py file. Then run a syncdb to create the new tables in database. Head over to the admin panel (or use the python API) and add x number of websites.
The problem now is your settings file needs to state what website it is going to be with the SITE_ID parameter. Therefore you need to make a separate settings file for each website and set the SITE_ID in each one, this can be found in the database under the contrib.sites app tables.
Finally to run with the settings file you need to add; settings=mysite.websiteASettings to the manage.py runserver command.
For deployment and running inside Apache you need to do this in your virtual host configuration. This is done like so;


    SetHandler python-program
    PythonHandler django.core.handlers.modpython
    SetEnv DJANGO_SETTINGS_MODULE mysite.websiteASettings
    PythonDebug On

(Borrowed from djangobook.com)

And it’s as easy as that, once you know how. Of course this isn’t always a good idea; it’s only something that really makes sense if both websites are going to be using very similar models. Now its nicely stored here for when I forget :)

*edit*
I should note that you don’t need to use the contrib.sites modules. However, it makes managing two websites much more easily. You can establish a many to many relationship for example and determine what content should be on each site and what should be on both. Very cool easy to do.

Leave a Reply