Settings

Last modified March 4, 2008 | Revision 4

A useful feature of using a regular python file for configuration in Django is that you can do regular python stuff in it. In particular i want a few different settings (mostly paths) on my development version to the live version, so i have a settings.py that is the live version, and a settings_dev.py that is and import and the differences eg:

from settings import *

TEMPLATE_DIRS = (
   ‘/development/path/templates/’,
)

# etc

Then the local apache setup for mod_python points to settings_dev rather than settings. Probably this is really obvious, and I should have thought of it significantly earlier. oh well.

Stupid domain based settings hack

import os, sys

def mixin(source, dest):
    for k,v in ((k, v) for k,v in source.__dict__.items() if not k[0] == ‘_’):
        setattr(dest, k, v)

def import_domain_settings(root=’domain_settings.’):
    self = sys.modules[__name__]
    node_parts = platform.node().split(‘.’)
    node_parts.reverse()

    parts_seen = []

    for part in node_parts:
        parts_seen.insert(0,part)
        settings = root + ‘-‘.join(parts_seen) 

        try:
            m = __import__(settings)
            mixin (m, self)
        except:
            pass

Put this into settings.py and you can then call import_domain_settings() to get it to try loading settings from the domain_settings package (starting with the lest specific domain proceeding to the most)

Not really something you would want to do in production code, but a fun hack anyway.

Last modified March 4, 2008 | Revision 4