Vinyll's blog

( Python, Javascript & Web stuff… )

Managing configuration files with Django

The trick here will be to keep the file containing all default values. Then, we'll create one file per environment that will get the values, and might override them, or add new (could even delete I guess). As we'll have multiple config files, we'll package them:

Configuration tree structure


Configuration file content

Your file will keep the default and customized values as usual. Other 3 files will have same content as below (all 3 similar).

Example with file ""

from conf.settings import *

    DEBUG = True
    OTHER_VAR = "value in local"

Update and

You should update these 2 files accordingly. will obviously refer to :

Point your DJANGO_SETTINGS_MODULE to conf.local :

#!/usr/bin/env python
import os
if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "conf.local")
    from import execute_from_command_line

Set DJANGO_SETTINGS_MODULE to conf.staging or depending on wich environment will call it :

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "conf.staging")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

Extra tips

Yes, that's all working already !

Install apps in local only

Some apps such as django-debug-toolbar should be installed and configured in local environment only. Here's how :

/project/conf/ : INSTALLED_APPS += ('debug_toolbar',) MIDDLEWARE_CLASSES += ('debug_toolbar.middleware.DebugToolbarMiddleware',)

Manage urls the same way

In most Django code you can read at the bottom of file something like :

Tricky usual code

if settings.DEBUG is True:
    urlpatterns += patterns('',
        (r"^static/(?P<path>.*)/?$", 'some.static.views')

That's great, that doesn't load that url if we did not activate DEBUG. But that's really tricky !

Clean way

In /projects/conf/, add :

ROOT_URLCONF = 'app.urls_local'

And create a app/ (or path accordingly), containing :

from urls import *
    urlpatterns += patterns('',
        (r"^static/(?P<path>.*)/?$", 'some.static.views')

(change "from urls …" accordingly to whatever package matches your file).

By vinyll on Jan. 16, 2012