[Solved]-Django-cms for multiple websites

10đź‘Ť

there are a few different options to manage different websites (and, thus, templates and page contents) in Django-cms.

The basic approach

My favorite is probably the simplest:

In my virtualenv I have a single django-cms installation AND a single “project” that contains ALL the templates I use.

I have a global settings file plus one for each website that does only import all global settings and set “SITE_ID”.

from base import *
SITE_ID = XXX

For structure i usually have a settings folder, an empty __init__.py inside, a base.py with all the common settings – including django-cms settings, and then the different websites eg. site1.py site2.py etc. (sometimes my structure is even slightly more complex to also account for dev/production, different machines, etc. but that’s not relevant here).

I launch each website as a different instance – I use gunicorn so that’s extremely easy too, each one of a different port.

I have an nginx fronted with a separate server configuration for each of my websites, and each of these points to a different gunicorn.

server {
    listen      80;
    server_name example1.com www.example1.com;
    ...
    location / {
        proxy_pass  http://localhost:PORT;
    }

}

Any of the gunicorn instances can access the admin, and all the data is shared in a single database, but for simplicity

That’s all!

Of course it can be done similarly with Apache, mod_wsgi and different virtualhosts.

Advanced

Themes

I actually structured my folders to have an apps folder called themes. Each theme is actually an APP, though mostly contains only the templates and static folders, and it’s added to the INSTALLED_APPS.
This allows for cute things such as inheritance and/or overriding between different themes.

Dynamic SITE_ID

It’s also possible to use a middleware that will dynamically extract and set the SITE_ID from the URL. This allows to have one single instance… but I don’t see any real advantage in this solution and rather find it a potential source of risks.

👤Stefano

Leave a comment