Django - Externalize

In Django, is where you store configuration such as database connection strings, directory paths for static and media resources, as well as caching settings. Obviously, these settings will be different between various developers, not to mention qa and production environments.

A typical solution is to branch, and then copy in the appropriate version after a deploy. This adds an extra moving piece that you may forget. Another solution is to put logic into to branch by host name, but this is a fairly brittle approach.

I have my file in an external directory. That way, it doesn't get checked into source control, or over-written by a deploy. I put this in the file under my Django project, along with any default settings:

import sys
import os.path

def _load_settings(path):
    print "Loading configuration from %s" % (path)
    if os.path.exists(path):
        settings = {}
        # execfile can't modify globals directly, so we will load them manually
        execfile(path, globals(), settings)
        for setting in settings:
            globals()[setting] = settings[setting]


Note: This is very dangerous if you can't trust