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.
There are several use cases for this approach. In our case, we'll create three settings files.
Here are the key steps.
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.
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
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 *
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')
:
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.
Run the following commands to make the statuses of the local computerand the server the same for the next steps.
git add .
git commit -m "updated settings structure"
git push origin master
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.
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.
There are several use cases for this approach. In our case, we'll create three settings files.
Here are the key steps.
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.
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
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 *
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')
:
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.
Run the following commands to make the statuses of the local computerand the server the same for the next steps.
git add .
git commit -m "updated settings structure"
git push origin master
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.