Recently I've had to expand a site for a client to support two domains using the same database. Thankfully, the django sites framework made this relatively painless. The documentation left a lot of (I think) crucially important information out, hence this hopefully informative post on the topic.
To clarify what I needed to accomplish, my client currently had one django project that powered a single site. They had the need to open up a new site, with essentially the identical site structure and functionality, just with different (and occasionally overlapping) data.
Old site file structure
website/
djangoproject/
__init__.py
settings.py
urls.py
views.py
templates/
specificapp1/
specificapp2/
static/
style.css
commonapps/
commonapp1/
commonapp2/
New site file structure
company/
website1/
djangoproject/
__init__.py
settings.py
urls.py
views.py
templates/
static/
style.css
website2/
djangoproject/
__init__.py
settings.py
urls.py
views.py
templates/
static/
style.css
companyapps/
specificapp1/
specificapp2/
commonapps/
commonapp1/
commonapp2/
The first real step I had to do here was abstract out the company's specific apps so that they weren't in the same namespace as the individual website. I wanted to do this so that each website's django project did not have to reference the other sibling django project.
The second major step was to incorporate the sites aspect into the existing apps. To accomplish this, first follow the steps here. For retrieving data throughout my site's code, it can be a pain to have to specify on every query to only retrieve content related to the current site, so I added a method to my custom model managers titled on_site, the definition of which looks something like the following:
class CustomModelManager(models.Manager):
def on_site(self):
return super(CustomModelManager, self).get_query_set().filter(sites__id=settings.SITE_ID)
That's pretty much it. Enjoy.