Last Updated: April 05, 2017
· sobolevn

Managing Django Settings

Before you start you will need to install django-split-settings with pip install django-split-settings. This helper provides a user-friendly interface to store your settings in different files. Let’s look at the example. Imagine you have an existing project with django, postgres, redis , rq, and emails.

That’s what your files would look like after adopting django-split-settings and some manual restructuring:

├── components
│   ├──
│   ├──
│   ├──
│   ├──
│   ├──
└── environments

That’s a clear separation of the settings based on two factors: what component they are configuring and at what environment we are working right now. And the flexibility of the library allows you to have any structure you want, not just the one described here.
In our settings/ we can define any logic we want. Basically, we would just define what kind of components we would like to use and select the environment. Here’s an example, we use in production for all our projects:

This is a django-split-settings main file.
For more information read this:

Default environment is `developement`.
To change settings file:
`DJANGO_ENV=production python runserver`

from import optional, include
from os import environ

ENV = environ.get('DJANGO_ENV') or 'development'

base_settings = [
    'components/',  # standard django settings
    'components/',  # postgres
    'components/',  # redis and redis-queue
    'components/',  # smtp

    # You can even use glob:
    # 'components/*.py'

    # Select the right env:
    'environments/' % ENV,
    # Optionally override some settings:

# Include settings:

And that’s it. Our application would run as usual. We have achieved multiple goals with so few lines of code:

  1. We now have separated settings based on what they configure. Gaining readability and maintainability
  2. We now have separated settings based on environment
  3. We now have optional local settings with now dirty hacks
  4. We did not have to do any refactoring except just some basic restructuring

Read full article: