Last Updated: February 25, 2016
·
597
· amolkher

Django Shared Settings.

We are big fans of Django Debug Toolbar. However very often we ran into a problem that we need to turn it on or off even in production quickly. Now our production and staging settings files are quite different. So we came up with a trick to organizing the settings files so that they can be enhanced with minimal long term disruption.

First we created a shared settings file that holds all the common settings between our production and staging servers.

In our settings file we import the shared settings file.

Next we import a developer settings file. However to include something like django debug toolbar it needs to be part of the INSTALLED_APPS list. The common way of doing this is to redefine it completely. But that means if we ever add a new django package we have to add it to everyone’s developer settings file.

So the solution we came up with was to let the extra settings file define a new property called ADDITIONAL_APPS which defined any additional apps you want only on your debug environment.

In our main settings file now we import any developer settings if present and then if it defines additional property we add it back to the INSTALLED_APPS list. We do this also for template loaders and context processors too.

This makes settings extensible and maintenance friendly over the long run.

Hope you find this trick useful.