Django Production Settings (1) – Settings.py for Development and Production
For the production environment, there are several development configurations that need to be adjusted. You'll need to continue using two different settings to manage the different environments.
One of the approaches to handling two or multiple settings is creating multiple settings files to switch configurations between the local environment and the production environment.
To manage highly confidential information, such as passwords or secret keys, you need to make a .env file and save the information separately from the settings.py file.
This lesson will explain how to create and manage multiple settings files.
1. Create the settings directory and setting files
There are several use cases for this approach. In our case, we'll create three settings files.
- base.py: the original settings.py (rename)
- development.py: use this for settings for development in the local environment
- production.py: use this for settings for production on the server
Here are the key steps.
1. Create the settings directory and save the three files under the directory
Create development.py and production.py. Rename settings.py to base.py. Create the settings directory. Save the three files under the settings directory.
The directory structure will be like the one below.
2. Update base.py
As the directory structure has changed, the BASE_DIR path should be updated. Add one more 'parent
' in the path like shown below.
BASE_DIR = Path(__file__).resolve().parent.parent.parent
3. Edit development.py and production.py
Add the following code. The code is the same for both files. With this code, development.py and production.py can overwrite the settings in base.py.
from .base import *
from .base import *
2. Update wsgi.py and manage.py
As the settings file path has changed, you need to adjust the file path to access the new settings files.
:
def main():
"""Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings.production')
try:
:
:
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings.production')
:
3. Check the result for development settings with the runserver command
To run the app using the local settings, add --settings config.settings.development
when you execute the runserver
command.
python manage.py runserver --settings config.settings.development
With the 'config.settings.local
' setting, you can see that the server is running.
System check identified no issues (0 silenced).
April 21, 2023 - 10:27:34
Django version 4.1.7, using settings 'config.settings.local'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
4. Commit and push the code to GitHub from the local computer and pull it from the server
Run the following commands to make the statuses of the local computer and the server the same for the next steps.
From the local
git add .
git commit -m "updated settings structure"
git push origin master
From the server
git pull
:
config/{settings.py => settings/base.py} | 2 +-
config/settings/development.py | 2 ++
config/settings/production.py | 2 ++
config/wsgi.py | 2 +-
manage.py | 2 +-
:
Check if the settings directory structure is the same as the one on the local computer.