Python Zone is brought to you in partnership with:

Chris has been married to the love of his life, Danielle, for over four years now and they recently had their first child, their daughter Helena. Chris was an expert wrangler of developer content for DZone, then spent some time in the marketing department for DZone and AnswerHub, where he taught himself to code html. Now he handles production duties for DZone, so he's responsible for all those emails you get from DZone! BTW, if you're looking for someone to bore you to death with endless football knowledge, Chris is your go-to-guy! Chris is a DZone employee and has posted 321 posts at DZone. You can read more from them at their website. View Full User Profile

Extending Django Settings for the Real World

  • submit to reddit

Originally Authored by Adam Nelson

A basic Django installation keeps its global variables in a file called This is perfect for simple deployment because it allows the developer to overwrite Django variables like INSTALLED_APPS or SESSION_ENGINE very easily. You simply update the variable like so:

SESSION_ENGINE = 'django.contrib.sessions.backends.cache'

From within the shell, you can see the result:

./ shell
>>> from django.conf import settings
>>> settings.SESSION_ENGINE

Many people have two environments in which they work, and therefore a typical file will have something like this at the end:

    from local_settings import *
except ImportError:

This overwrites variables from a file called, overriding any existing variables in the file. Try it. Add the import code above into your file and create a new file called in the same directory as the file and add this to it:

SESSION_ENGINE = 'django.contrib.sessions.backends.cached_db'

Now, if you enter the shell like you did above and request settings.SESSION_ENGINE, you’ll get ‘django.contrib.sessions.backends.cache’. This is very handy because, in a typical situation, you can have a file which works for all your environments and then have a file for each environment that overrides the variable values.

Problems with the Standard Settings files

Unfortunately, in this scenario, variables from the file cannot be interpreted in the file and therefore, you couldn’t do something like this:

INSTALLED_APPS += ('debug_toolbar',)

In this situation, you’ll get a NAME_ERROR in which INSTALLED_APPS is undefined rather than  (‘django.contrib.auth’,’debug_toolbar’,).

A Modest Proposal

What we do at Yipit is to put all of our variables in a settings directory:

settings/ (where the variable for all environments are) (optional - defines the environment we're in - not under version control) (shared by all the development environments) (live site)

This allows us to create an file for all the variables that are the same across all environments. The file requires no imports (except whatever you may need from Python itself, or other libraries). Then, each file imports from in the way you might imagine:

from settings import *
#Alter or add production specific variables

from settings import *
#Alter or add development specific variables

from settings.development import *
#This file denotes which environment we're in.
#This file creates a development environment

Note: If you’re not that familiar with Python, ‘from settings’ accesses settings/

In more complex scenarios, you may also want to inherit settings from files other than setting/, and this system fully supports that option. For example, you may have a settings/ files that pulls from settings/ and then settings/ could pull from staging. It’s really up to you.

This approach has some shortcomings, notably that you can’t dynamically change variables - but that’s really not the point of settings. Now, you can change variables on a per-environment basis like this (in, say,

INSTALLED_APPS += ('debug_toolbar',)

Which will set INSTALLED_APPS as (‘django.contrib.auth’,’debug_toolbar’,). Here is our file:

#!/usr/bin/env python
import sys
import traceback
from os.path import abspath, dirname, join

from import execute_manager

SETTINGS_ACTIVE_CONTENTS = "\033[1;32mfrom settings.local import *\033[1;33m"
    from settings import active as settings
except ImportError, e:
    print '\033[1;33m'
    print "Apparently you don't have the file settings/ yet."
    print "Create it containing '%s'\033[0m" % SETTINGS_ACTIVE_CONTENTS
    print "=" * 20
    print "original traceback:"
    print "=" * 20

sys.path.insert(0, abspath(join(dirname(__file__), "../")))
sys.path.insert(0, join(settings.PROJECT_ROOT, "apps"))

if __name__ == "__main__":

Final Thoughts

If we make further changes to our settings configuration, we’ll do a follow-up post. Some modifications we are considering:

  • Using Chef to hold many of the systemwide configuration parameters (usernames, machine addresses, etc…) in order to move that information away from the application layer and onto the environment layer.

  • Creating an additional settings file that imports for calculated settings. For example, if a read replica database has not been declared, but the application expects one, have the default database act as the read replica.

If you have a different way of handling settings, we would love to hear from you in the comments below.

Adam Nelson is CTO at Yipit.