Django and Settings Injection

Last Updated: 16 January 2015

django injection python

This article is a work in progress, or documents a feature that is not yet released to all users. This article is unlisted. Only those with the link can access it.

Table of Contents

On January 1, 2013, all Django applications will no longer have code automatically injected into settings.py.

Reasoning

Having code automatically injected into your settings.py makes your deployment unportable, mysterious, and frustrating when you stray from the defaults.

The Python community values declarative syntax. As PEP 20 states:

Explicit is better than implicit.

It is also a 12 Factor violation:

Config varies substantially across deploys, code does not.

Your code should honor the DATABASE_URL environment variable no matter where it is deployed, not just on Heroku.

The dj-database-url module reads the DATABASE_URL environment variable and converts it to the configuration dictionary that Django requires.

To use it, add the following to requirements.txt:

dj-database-url==0.2.1

And the following to the bottom of your settings.py:

import dj_database_url
DATABASES = {'default': dj_database_url.config(default='postgres://localhost')}

Try it out today

You can disable settings injection today by adding a file to your repository:

$ touch .heroku/injection_disabled

We recommend testing this on your Django applications before January 1, 2013.