How to configure Environment Specific settings in Python Django Framework?

For beginners, whenever you type python runserver it takes all the configurations from has all the configurations such as the installed applications, middleware used, templates, languages, time zone, static folder’s location, etc. Generally, we shouldn’t be concerned about the file. Because Django Framework provides a boilerplate or base configuration that runs the application in the local environment.

Why do we need Environment Specific Settings?

There are certain configurations in your such as DEBUG, STATIC_URL, DATABASES, SECRET_KEY, ALLOWED_HOSTS, etc. These settings are actually the ones that you need to look after while deploying to the production environment. Whenever your application goes live, you shouldn’t display your debug logs, or error messages on the browsers. This will make your application vulnerable. Also you might want to put different secret keys.

For these reasons, we need environment-specific settings. In this tutorial, I will be creating different settings for local, development, and production environment. Basically, the following things will be covered in this tutorial:

  1. Create a base setting as which is the default setting for the local environment.
  2. Create a for development environment
  3. Create as settings for the production environment

After that I will show the command to select specific settings as per your environment.

Step1: Rename your as

First rename your file. Since we are trying to create multiple settings for different environments, we wont be needing Rename it but donot delete it now. We will need all of its contents in other settings file

Step2: Create a Directory “settings”

Inside your project directory where initially there exists create a folder named “settings“.

Step3: Create inside the settings directory

Here we will call all three settings files and their configurations. Add the following code in file:

from .base import *

from .development import *

from .production import *

Step4: Create three settings files for local, development, and production environment.

Inside the settings, folder creates three files named,, and At copy the contents of file. Initially, we need to make a simple modification in

Initially the base directory for is mentioned in BASE_DIR configuration as shown below:

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

Now that we have created a folder and added all the settings files inside that folder we need to point the base directory one level down. i.e. we need to add os.path.dirname() in BASE_DIR as shown below:

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

Step5: Make some configuration changes in any settings file

Now that we have made files for specific environments lets make some modification to see if the change gets reflected. In modify following settings:

DEBUG = False
ALLOWED_HOSTS = [‘’, ‘’]

Step5: Run your project from commandline

After changing the configurations in run your project from the terminal with following command:

python runserver --settings=projectname.settings.production

Rename projectname with your actual project name and run the code. Now whenever you will have any error in the application it will not display the error logs. It is because the DEBUG mode is disabled from the setting of production environment. Also If we hadn’t add as allowed hosts, then although the program is running none of the web request will be accepted by the project.