Using multiple settings file for a django project

When I use django for development, I usually need multiple versions of settings.py file for my project.One for development and one for production. My current approach to solve multiple settings file problem is using tree files. In my project directory I have following files:

  • settings_default.py
  • settings.py
  • settings_local.py

settings_default.py:

This file contains global values. I usually rename original settings.py file as settings_default.py.

settings.py

This file will have following content

# settings.py
from settings_default import *
try:
    from settings_local import *
except:
    # if there is no settings_local file, just use default values
    pass

This file gets its values from settings_default.py and override them with settings_local.py if it exists.

settings_local.py

This file will have global specific settings for each environment.

from settings_default import INSTALLED_APPS

DEBUG = True
TEMPLATE_DEBUG = DEBUG

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': 'sample.db',
        'USER': '',
        'PASSWORD': '',
        'HOST': '',
        'PORT': '',
    }
}

# Add an extra app to installed apps
# Notice how we include INSTALLED_APPS from settings_default
INSTALLED_APPS += (
    'django_extensions',
    )

Sample usage for this setup can be found in python blog engine

blog comments powered by Disqus