Django Settings – Disqus Style

David Cramer posted this excellent article back in 2011 about how they handle their config files. In a multi-environment setup (dev, stage, prod) this is paramount.

In his example he makes use of an environment variable to set which configuration to use. While this is probably ideal, it’s not always an option. As a replacement, you could use the IP address or host name of the machine:

settings.py

HOST_IP_ADDRESS = socket.gethostbyname(socket.gethostname())
HOST_NAME = socket.gethostname()
if string.find(HOST_NAME, '.local') is not 1:
DJANGO_CONF = 'dev'
elif HOST_IP_ADDRESS is '123.123.123.123':
DJANGO_CONF = 'staging'
else:
DJANGO_CONF = 'prod'
# import from the selected environment (i.e. environments.dev)
module = __import__('environments.' + DJANGO_CONF, globals(), locals(), ['*'])
for k in dir(module):
locals()[k] = getattr(module, k)

view raw
settings.py
hosted with ❤ by GitHub

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s