As noted earlier, you will be creating new features in the development environment, testing them in the testing environment, and then putting the website onto a staging server to let other people try the new features. Then, the website will be deployed to the production server for public access. Each of these environments can have specific settings, and you will learn how to organize them in this recipe.
Configuring settings for development, testing, staging, and production environments
Getting ready
In a Django project, we'll create settings for each environment: development, testing, staging, and production.
How to do it...
Follow these steps to configure the project settings:
- In the myproject directory, create a settings Python module with the following files:
- __init__.py makes the settings directory a Python module.
- _base.py for shared settings
- dev.py for development settings
- test.py for testing settings
- staging.py for staging settings
- production.py for production settings
- Copy the contents of settings.py, which was automatically created when you started a new Django project, to settings/_base.py. Then, delete settings.py.
- Change the BASE_DIR in the settings/_base.py to point one level up. It should first look as follows:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
After changing it, it should look like the following:
BASE_DIR = os.path.dirname(
os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
)
- If the settings of an environment are the same as the shared settings, then just
import everything from _base.py there, as follows:
# myproject/settings/production.py
from ._base import *
- Apply the settings that you want to attach or overwrite for your specific environment in the other files—for example, the development environment settings should go to dev.py, as shown in the following code snippet:
# myproject/settings/dev.py
from ._base import *
EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"
- Modify the manage.py and myproject/wsgi.py files to use one of the environment settings by default by changing the following line:
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
- You should change this line to the following:
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings.production')
How it works...
By default, the Django management commands use the settings from myproject/settings.py. Using the method that is defined in this recipe, we can keep all of the required nonsensitive settings for all environments under version control in the config directory. On the other hand, the settings.py file itself would be ignored by version control and will only contain the settings that are necessary for the current development, testing, staging, or production environments.
For each environment, it is recommended that you set the DJANGO_SETTINGS_MODULE environment variable individually, either in PyCharm settings, the env/bin/activate script, or in .bash_profile.
See also
- The Working with Docker containers for Django, Gunicorn, Nginx, and PostgreSQL recipe
- The Handling sensitive settings recipe
- The Defining relative paths in the settings recipe
- The Creating a Git ignore file recipe