While working on my CRM, I had a bit of an issue: I wanted to keep my setup specific settings (Database information, timezone, secret key, etcs) separate from application settings (INSTALLED_APPS, ROOT_URLCONF, etc). This is useful since it keeps development configurations and production configurations separate and it keeps your setup specific settings out of your VCS. Well, less talk and more code!

# local_settings.py
ADMINS = (
    ('Your Default User', 'nobody@example.com'),
)
MANAGERS = ADMINS

PRODUCTION = False
TIME_ZONE = 'America/Denver'
LANGUAGE_CODE = 'en-us'
SITE_ID = 1
USE_I18N = False
SECRET_KEY = 'PutASecretKeyHere'

DATABASE_ENGINE = ''
DATABASE_NAME = ''
DATABASE_USER = ''
DATABASE_PASSWORD = ''
DATABASE_HOST = ''
DATABASE_PORT = ''

PROJECT_ROOT = '/path/to/your/project'
MEDIA_URL = 'http://your/media/path/'
# Settings.py
from local_settings import *
if not PRODUCTION:
    DEBUG = True
    TEMPLATE_DEBUG = DEBUG
else:
    DEBUG = False
    TEMPLATE_DEBUG = False

MEDIA_ROOT = '%s/templates/media' % (PROJECT_ROOT)
ADMIN_MEDIA_PREFIX = '/media/'

TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.load_template_source',
    'django.template.loaders.app_directories.load_template_source',
)

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.core.context_processors.auth',
    'django.core.context_processors.debug',
    'django.core.context_processors.i18n',
    'django.core.context_processors.media')

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
)

TEMPLATE_DIRS = (
    '%s/templates' % (PROJECT_ROOT)
)

INSTALLED_APPS = (
     'django.contrib.auth',
     'django.contrib.contenttypes',
     'django.contrib.sessions',
     'django.contrib.sites',
     'django.contrib.admin',
)

from post_settings import *
# post_settings.py

TEMPLATE_DIRS.append("/an/additional/template/path/")

So, how does it work? Quite simply. Put your database settings into local_settings.py and any other settings that aren’t set in settings.py will go there as well. Need to override something in settings.py? Use post_settings.py for that. Not cool enough? In local_settings.py, use a conditional to setup your database settings! This will look something like this:

if PRODUCTION:
    DATABASE_TYPE = 'mysql'
    [...]
else:
    DATABASE_TYPE = 'sqlite3'
    [...]

Pretty nifty, eh? I figured it might make someone’s life a little easier.

Cheers!

Nick