Multiple shared codebase and DB Django sites

I created Django proyect with 20 sites (one domain per site) for 20 different countries. The sites have everything: codebase, database, urls, templates, etc.

The only thing they don’t share is the small settings (logo, CSS theme background color, language code, etc.) that I set in each site settings file (each site has one settings file and all of these files are imported global settings file with ordinary material). Right now, to run sites in development mode, I will do:

django-admin.py runserver 8000 --settings=config.site_settings.site1 django-admin.py runserver 8001 --settings=config.site_settings.site2 ... django-admin.py runserver 8020 --settings=config.site_settings.site20 

I have a couple of questions:

  • I read that you can create a virtual host for each site (domain) and transfer the settings.py file of the site to it. However, I am afraid that this will create one instance of Django for each site. I'm right?
  • Is there a more efficient deployment method? I read about django-dynamicites , but I'm not sure if this is the right way.
  • If I decide to deploy using Heroku, it seems that Heroku expects only one settings file for each application, so I will need to have 20 applications. Is there a solution for this?

Thanks!

+8
django heroku django-sites
source share
1 answer

So, I recently did something similar, and found that the strategy below is the best option. I assume you are familiar with git branches at this point, as well as with Heroku remotes. If you do not, you should first read this: https://devcenter.heroku.com/articles/git#multiple-remotes-and-environments

The main strategy that I accept is to have one codebase (single git repo) with:

  • A master , which contains all of your common code: templates, views, URLs.
  • There are many master based site branches that contain all the settings for the site: css, images, settings files (if they are very different).

How it works like this:

First make sure you are in the master branch.

Secondly, create a new git branch for one of your domains, for example: git checkout -b somedomain.com .

Third, configure the somedomain.com branch so that it looks the way you want.

Then deploy somedomain.com live to Heroku by running heroku create somedomain.com --remote somedomain.com .

Now push your branching code somedomain.com into the new Heroku app: git push somedomain.com somedomain.com:master . This deploys your Heroku code.

Now that you have the somedomain.com branch deployed with your Heroku application, you can do all the normal Heroku stuff by adding --remote somedomain.com to your regular Heroku commands, for example:

  • heroku pg:info --remote somedomain.com
  • heroku addons:add memcache:5mb --remote somedomain.com
  • and etc.

So now you basically have two branches: the master branch and the somedomain.com branch.

Return to the master branch and create a new branch for the following domain: git checkout master; git checkout -b anotherdomain.com git checkout master; git checkout -b anotherdomain.com . Then customize it to your liking (css, material for a specific site) and expand the same as we above.

Now I'm sure you can see where this is happening. We have one git branch for each of our custom domains , and each domain has its own Heroku application. The advantage (obviously) is that each of these project settings is based on the master branch, which means that you can easily make updates for all sites at the same time.

Suppose you updated one of your views in your master branch - how can you deploy it to all of your sites at once? Easy!

Just run:

  • git checkout somedomain.com
  • git merge master
  • git push somedomain.com somedomain.com:master # expand the changes

And repeat for each of your domains. In my environment, I wrote a script that does this, but it's easy enough to do it manually if you want.

Anyway, hope this helps.

+6
source share

All Articles