When developing a Django application you will most likely need to have specific settings for each environment. It’s either you have complex deployment stages or just a local and a production environment you will have to have a good way to divide the settings.

There are several ways of achieving the end results, however my favorite one is having separate settings files.

Split
1
settings.py
into several files

The common Django 1.6 directory structure looks a lot like this:

- project/
   - manage.py
   - requirements.txt
   - project/
      - __init__.py
      - urls.py
      - wsgi.py
      - settings.py
   - app1/
   - app2/

You need to create a

1
settings
directory inside
1
project/project
. In the newly created directory, you need to create a
1
__init__.py
file so that the python module can be imported.

Creating

1
base.py
is desirable for declaring settings that are environment agnostic.

In this example we have a development and a production environment. Create

1
local.py
for development and
1
heroku.py
for production settings.

The new directory structure for the Django application looks like this:

- project/
   - manage.py
   - requirements.txt
   - project/
      - __init__.py
      - urls.py
      - wsgi.py
      - settings/
      	  - __init__.py
          - base.py
          - local.py
          - heroku.py
   - app1/
   - app2/

Move the settings across the newly created files

Having the settings divided it’s a matter of preference and necessity. I usually have the database credentials set for each environment.

Add the import for the base settings in each of the different env files:

from base import *

For detailed Heroku settings for Django check https://devcenter.heroku.com/articles/getting-started-with-django#django-settings.

Set the settings path

Locally there are several ways for setting the path for the settings module. You can either set the environment variable

1
DJANGO_SETTINGS_MODULE
:

export DJANGO_SETTINGS_MODULE=project.settings.local

Or assign the

1
--settings
flag when starting the development server:

python manage.py runserver --settings=project.setttings.local

On production environments it’s pretty much the same. The only thing that changes is the level of access you have. On Heroku for example you can not connect via SSH or have a configuration file for environment variables, however the command line tool allows you to set environment variables:

heroku config:set DJANGO_SETTINGS_MODULE=projects.settings.heroku

Remove the default assignment for
1
DJANGO_SETTINGS_MODULE

The

1
DJANGO_SETTINGS_MODULE
setting is overwritten in
1
wsgi.py
and
1
manage.py
. Open
1
project/project/wsgi.py
and
1
project/manage.py
and remove the lines that assign the default value:

...
import os
...
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")
...

The introductory post on the new blog. Nothing much to say really, maybe just mention that Jekyll is great and you can find the source code for the blog on Github at morion4000/morion4000.github.io.