Skip to content

Managing settings for multiple environments in django

In real-world projects, you will have to deal with multiple environments. we have at least a local environment for development and a production environment for serving your application.

we will use a base file that defines common settings, and a settings file per environment that overrides any necessary settings and defines additional ones.

Create a settings/ directory next to the settings.py file of the your project. Rename the settings.py file to base.py and move it into the new settings/ directory.

Create the following additional files inside the settings/ folder so that the new directory looks as follows:

settings/
    __init__.py
    base.py
    local.py
    prod.py

These files are as follows:

  • base.py: The base settings file that contains common settings (previously settings.py)
  • local.py: Custom settings for your local environment
  • prod.py: Custom settings for the production environment

You have moved the settings files to a directory one level below, so you need to update the BASE_DIR setting in the settings/base.py file to point to the main project directory.

When handling multiple environments, create a base settings file and a settings file for each environment. Environment settings files should inherit the common settings and override environment-specific settings.

Edit the settings/base.py file and replace the following line:

BASE_DIR = Path(__file__).resolve().parent.parent

with the following one:

settings/base.py

BASE_DIR = Path(__file__).resolve().parent.parent.parent

You point to one directory above by adding .parent to the BASE_DIR path. Let’s configure the settings for the local environment.


Local environment settings

Instead of using a default configuration for the DEBUG and DATABASES settings, you will define them for each environment explicitly. These settings will be environment specific. Edit the settings/local.py file and add the following lines:

local.py
from .base import *

DEBUG = True

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}

This is the settings file for your local environment. In this file, you import all settings defined in the base.py file, and you define the DEBUG and DATABASES settings for this environment. The DEBUG and DATABASES settings remain the same as you have been using for development.

Now remove the DATABASES and DEBUG settings from the base.py settings file.

Django management commands won’t automatically detect the settings file to use because the project settings file is not the default settings.py file. When running management commands, you need to indicate the settings module to use by adding a --settings option, as follows:

Python

python manage.py runserver --settings=educa.settings.local

Next, we are going to validate the project and the local environment configuration.


Defining environment variable

If don’t want to pass the --settings option every time you run a management command, you can define the DJANGO_SETTINGS_MODULE environment variable. Django will use it to identify the settings module to use.

If you are using Linux or macOS, you can define the environment variable by executing the following command in the shell:

Linux

export DJANGO_SETTINGS_MODULE=educa.settings.local

To remove an environment variable in Linux, including one set with the syntax export VARIABLE="value", you can unset it using the unset command.

Linux

unset VARIABLE

If you are using Windows, you can execute the following command in the shell:

Windows

set DJANGO_SETTINGS_MODULE=educa.settings.local

Any management command you execute after will use the settings defined in the DJANGO_SETTINGS_MODULE environment variable.


Production environment settings

Let’s start by adding initial settings for the production environment. Edit the settings/prod.py file and make it look as follows:

prod.py
from .base import *

DEBUG = False

ADMINS = [
    ('Antonio M', 'email@mydomain.com'),
]

ALLOWED_HOSTS = ['*']

DATABASES = {
    'default': {
    }
}

These are the settings for the production environment:

  • DEBUG: Setting DEBUG to False is necessary for any production environment. Failing to do so will result in the traceback information and sensitive configuration data being exposed to everyone.
  • ADMINS: When DEBUG is False and a view raises an exception, all information will be sent by email to the people listed in the ADMINS setting. Make sure that you replace the name/email tuple with your own information.
  • ALLOWED_HOSTS: For security reasons, Django will only allow the hosts included in this list to serve the project. For now, you allow all hosts by using the asterisk symbol, *. You will limit the hosts that can be used for serving the project later.
  • DATABASES: You keep default database settings empty because you will configure the production database later.

Reference