Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Django settings file as loader for server specific settings files
# 1. Imports base settings from `/serversettings/`
# 2. Imports settings based on server name, unless:
# a. The environmental setting `SERVER_TYPE` has been set in the `django.wsgi` script
# b. The name of the server corresponds to a name in the `DEVELOPMENT_MACHINES` tuple
import os
import platform
import sys
import warnings
from django.utils.importlib import import_module
DEVELOPMENT_MACHINES = ('christian-mac',)
TESTING_MACHINES = ('alpha','beta',)
def get_server_name():
server_type = os.environ.get('SERVER_TYPE','')
server_name = platform.node().split('.')[0]
if server_type:
return server_type
if server_name in DEVELOPMENT_MACHINES:
return 'local'
if server_name in TESTING_MACHINES:
return 'testing'
if server_name in PRODUCTION_MACHINES:
return 'production'
return server_name
def override_settings(dottedpath):
_module = import_module(dottedpath)
except ImportError, e:
warnings.warn("Failed to import %s" % dottedpath)
warnings.warn("Exact error was: %s" % e)
_thismodule = sys.modules[__name__]
for _keyword in dir(_module):
if _keyword.isupper() and not _keyword.startswith('__'):
setattr(_thismodule, _keyword, getattr(_module, _keyword))
override_settings('mysite.serversettings.%s' % get_server_name())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment