For beginners, whenever you type python manage.py runserver it takes all the configurations from settings.py. settings.py 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 settings.py 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 settings.py 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:
- Create a base setting as base.py which is the default setting for the local environment.
- Create a development.py for development environment
- Create production.py as settings for the production environment
After that I will show the command to select specific settings as per your environment.
Step1: Rename your settings.py as settings_backup.py
First rename your settings.py file. Since we are trying to create multiple settings for different environments, we wont be needing settings.py. 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 settings.py create a folder named “settings“.
Step3: Create __init__.py inside the settings directory
Here we will call all three settings files and their configurations. Add the following code in __init__.py 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 base.py, development.py, and production.py. At base.py copy the contents of settings.py file. Initially, we need to make a simple modification in base.py.
Initially the base directory for settings.py 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 production.py modify following settings:
DEBUG = False
ALLOWED_HOSTS = [‘0.0.0.0’, ‘127.0.0.1’]
Step5: Run your project from commandline
After changing the configurations in production.py run your project from the terminal with following command:
python manage.py 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 127.0.0.1 as allowed hosts, then although the program is running none of the web request will be accepted by the project.