#Django Multiple Modular Configuration
Enables modular settings.py configurations and configuration mix-ins for your django project. With any Django project your settings.py file quickly grows beyond managability. A modular approach is needed and that's what this Gist does for you.
##Installation
- create package
config
- add this gist's
config_base.py
- create a global configuration class
EnvSettings_GLOBAL(EnvSettingsBase)
- from your
settings.py
cut/paste all global settings into the new class - add the following two lines in
settings.py
:
os.environ.setdefault('DJANGO_CONFIGURATION', 'EnvSettings_GLOBAL')
EnvSettingsBase.setup(globals(), os.environ['DJANGO_CONFIGURATION'])
##Usage Whenever you see the need for a specific configuration (such as per development, test and production environment), create a new class:
class EnvSettings_Local(EnvSettings_GLOBAL):
SOME_SETTING = `value`
Enable this new configuration using the process' environment, e.g.
export DJANGO_CONFIGURATION=EnvSettings_Local
###Mix-ins To collect all settings applicable to a specific extension or for a specific situation, simply create a mix-in class. The following example collects all settings to enable site-wide HTTPs using the SecureRequiredMiddleware:
from config.env_global import EnvSettings_GLOBAL
class Config_SiteHttps(object):
#A configuration mix in to enable full site encryption
#Overred the SECURE_REQUIRED_PATHS to set sub-site paths only
#
#MAKE SURE YOU LIST THE MIXIN *BEFORE* THE PARENT CLASS, e.g.
#
#class EnvSettings_SecureSite(Config_SiteHttps, EnvSettings_GLOBAL):
# pass
#
# setup the middleware for HTTPs. Secure middle ware must be in the
# beginning!
SSL_MIDDLEWARE_CLASSES = ('middleware.SecureRequiredMiddleware',)
MIDDLEWARE_CLASSES = SSL_MIDDLEWARE_CLASSES
MIDDLEWARE_CLASSES += EnvSettings_GLOBAL.MIDDLEWARE_CLASSES
# set the paths to be secured -- defaults to all requests
HTTPS_SUPPORT = True
SECURE_REQUIRED_PATHS = ('*',)