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
1 | settings.py |
The common Django 1.6 directory structure looks a lot like this:
You need to create a
directory inside 1
settings
. In the newly created directory, you need to create a 1
project/project
file so that the python module can be imported.1
__init__.py
Creating
is desirable for declaring settings that are environment agnostic.1
base.py
In this example we have a development and a production environment. Create
for development and 1
local.py
for production settings.1
heroku.py
The new directory structure for the Django application looks like this:
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:
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
Or assign the
flag when starting the development server:1
--settings
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:
Remove the default assignment for 1
DJANGO_SETTINGS_MODULE
1 | DJANGO_SETTINGS_MODULE |
The
setting is overwritten in 1
DJANGO_SETTINGS_MODULE
and 1
wsgi.py
. Open 1
manage.py
and 1
project/project/wsgi.py
and remove the lines that assign the default value:1
project/manage.py