Settings management

It is often necessary for a Django project to have different settings depending on the system environment, such as development, test, staging/acceptance and production. However, even on a single computer it can be helpful to have an easy way to switch between settings (e.g. to quickly simulate different environments).

django-environments helps you manage different Django settings within a Django project, and easily select settings from the command line or from WSGI, all with “maximum DRY™”.

Definining your settings

Instead of using a single settings.py module, django-environments expects you to organize your settings in a Python package, for example:

mysite/
    settings/
        __init__.py
        env/
            __init__.py
            development.py
            production.py
        base.py

Additionally, it suggests (but does not dictate) you use an inheritance/generalization model, simply by “inheriting” from more generic settings using from <package> import *, and overruling the settings as needed:

# mysite/settings/base.py
from djenv.settings import *

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'mysite',
)

# mysite/settings/env/development.py
from mysite.settings.base import *

DEBUG = True

INSTALLED_APPS += (
    'debug_toolbar',
)

Available settings modules

djenv.settings.core

The only required settings to include in order to use django environments.

This module defines four basic settings which are specific to django-environments:

djenv.settings.core.PROJECT_ROOT = '/path/to/your/project'

The full path to the root directory of your Django project.

This is useful for defining other settings such as STATIC_ROOT, and is used by the cdroot command.

djenv.settings.core.PROJECT = 'project'

The basename of the PROJECT_ROOT.

djenv.settings.core.DJANGO_PROJECT = 'mysite'

The basename of the directory containing your Django project’s ROOT_URLCONF (urls.py).

djenv.settings.core.DJANGO_PROJECT_DIR = '/path/to/your/project/mysite'

The full path to the DJANGO_PROJECT directory.

djenv.settings.generic

Sets some basic “sane defaults” for various Django settings.

Settings defined here include the ROOT_URLCONF, STATIC_ROOT, STATIC_URL and basic MIDDLEWARE_CLASSES.

djenv.settings.generic.LOCAL_SERVER_PORT = 8001

The HTTP port used when running the Django development server using the runserver command.