Chapter 6. Deploy Django App

Django Production Settings (1) – Settings.py for Development and Production

Django Production Settings (1) – Settings.py for Development and Production
Tag:

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.

Django-App-Production-Configuration-1--Settingspy-for-Development-and-Production

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.

config/settings/base.py
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.

config/settings/development.py
from .base import *
config/settings/production.py
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.

manage.py
 :
def main():
    """Run administrative tasks."""
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings.production')
try:
:
config/wsgi.py
 :
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.

Command Line - INPUT
python manage.py runserver --settings config.settings.development

With the 'config.settings.local' setting, you can see that the server is running.

Command Line - RESPONSE
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 computerand the server the same for the next steps.

From the local

Command Line - INPUT
git add .
git commit -m "updated settings structure"
git push origin master

From the server

Command Line - INPUT
git pull
Command Line - RESPONSE
 :
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.

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.

Django-App-Production-Configuration-1--Settingspy-for-Development-and-Production

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.

config/settings/base.py
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.

config/settings/development.py
from .base import *
config/settings/production.py
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.

manage.py
 :
def main():
    """Run administrative tasks."""
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings.production')
try:
:
config/wsgi.py
 :
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.

Command Line - INPUT
python manage.py runserver --settings config.settings.development

With the 'config.settings.local' setting, you can see that the server is running.

Command Line - RESPONSE
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 computerand the server the same for the next steps.

From the local

Command Line - INPUT
git add .
git commit -m "updated settings structure"
git push origin master

From the server

Command Line - INPUT
git pull
Command Line - RESPONSE
 :
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.

Tag: