Using Environment Variables in Django

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 and then use os.environ dictionary to pick up the values you need.

import os


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'