Using Environment Variables in Django Settings.py

June 25, 2010

Our release team like environment variables. It would be nice to deploy a Django site where there are no code changes and all config is handled through environment variables.

Thankfully, it's straightfoward, just add an import to settings.py and then use os.environ dictionary to pick up the values you need.

import os

DATABASE_USER = os.environ['DATABASE_USERNAME']

This code will look for an environment variable called DATABASE_USERNAME and use the value for Django's database user setting.

To set an environment variable on bash, use export:

export DATABASE_USERNAME = 'drumcoder'