Multiple settings and requirements file in Django

Sandeep Singh Sambyal
3 min readJul 4, 2021

--

The Django framework has hundreds of settings available to be controlled from its settings modules, although most of them come with default values. However, we can adjust them based on our needs. Since settings are loaded when a server starts, changing them in the real-world would require a server restart, which is not recommended in production.

The settings can become quite complex depending on the project. When it comes to setting files, we should always follow 3 rules:
1. All settings files should be version-controlled: All changes in settings should be tracked.
2. DRY principle: Don’t repeat yourself principle states that we should always inherit values from the base settings file rather than copy pasting values in multiple places.
3. Safeguard Secret Keys: Your secret keys should not be part of version control systems.

Use Multiple settings file:

Instead of having a single settings file, use a setup that has multiple settings files depending on the environment.

Project Structure

With this multiple settings file method, we first need to create a folder that contains __init__.py in it. Create settings files according to your needs. The main settings.py file in your project directory can contain all the basic settings, and all the environment-specific files can be placed in this folder. As seen in the image above, we have created settings_config as the folder with local_settings.py and staging.py with the settings file as per the need.

Configuring new settings files:

The following two things should be noted and addressed, as they are frequently observed as common mistakes:

1. Import everything from base settings.py: In our case we have our default settings.py file as the base settings file: Sample_Test_Project/settings.py. This file should be imported in all new files.

2. Capitalize variables in settings files: When you define variables in settings files, they should always be capitalized.

from Sample_Test_Project.settings import *VALUE = 100
DEBUG = True

Run server with new setting files:

Depending on the environment now we can use our respective settings file to start the server.

python manage.py runserver 8080 –settings=Sample_Test_Project.settings_config.local_SettingsORpython manage.py runserver 8080 --settings=Sample_Test_Project.settings_config.staging

Using Multiple requirements files:

We will use the same approach as we did in the settings file when using multiple requirements files.

requirements/
base.txt
local.txt

We create a folder called requirements in which we can create multiple requirements files based on our needs. In this folder, we create a base.txt with all the common required packages and then requirements files specific to the environment.

Requirements file image

Installation from file:

pip3 install -r requirements/local.txt

Conclusion:

Here we saw how to use multiple settings and requirements file in your Django project. If you have any queries please feel free to contact.

--

--

Sandeep Singh Sambyal
Sandeep Singh Sambyal

No responses yet