

Managing Django’s settings
source link: https://sobolevn.me/2017/04/managing-djangos-settings
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Managing Django’s settings
April 1, 2017 4 mins read
Start a discussion Edit this page

Managing Django’s settings might be tricky. There are severals issues which are encountered by any Django developer along the way.
First one is caused by the default project structure. Django clearly offers us a single settings.py
file. It seams reasonable at the first glance. And it is actually easy to use just after the start. But when it comes to the real-world it only causes misunderstanding and frustration.
At some point, you will need to put some kind of personal settings in the main file: certificate paths, your username or password, database connection, etc. But putting your user-specific values inside the common settings is a bad practice. Other developers would have other settings, and it would just not work for all of you. The most known hack for this situation is local_settings.py
. This file is placed near the regular settings file and ignored from version control. There are also these lines which are usually put somewhere in the end of settings.py
:
try:
from local_settings import *
except ImportError:
# No local settings was found, skipping.
pass
Looks pretty straight-forward. It is also sometimes accompanied with local_settings.py.template
, which is version controlled, to keep your local settings structure up to date.
You would definitely need production settings sometime soon. How would you do that? Create a new file. Do you need special settings for testing? Create a new file. Staging? New file. You would have a lot of files.
Secondly, when you have a lot of things to configure, your settings files will become long and heavy. At this point you would start to think: maybe I could separate these values into different files and reuse them at different environments? If this thought has ever come to your mind — you should give django-split-settings a try.
Usage
How does django-split-settings
solve these issues? This helper provides a user-friendly interface to store your settings in different files. Let’s look at the example. Imagine you have an existing project with django
, postgres
, redis
, rq
, and emails.
Before we start, let’s install django-split-settings
with:
pip install django-split-settings
That’s what your files would look like after adopting django-split-settings
:
your_project/settings/
├── __init__.py
├── components
│ ├── __init__.py
│ ├── database.py
│ ├── common.py
│ ├── emails.py
│ ├── rq.py
└── environments
├── __init__.py
├── development.py
├── local.py.template
├── production.py
└── testing.py
That’s a clear separation of the settings based on two factors: what component they are configuring and at what environment we are working right now. And the flexibility of the library allows you to have any structure you want, not just the one described here.
In our settings/__init__.py
we can define any logic we want. Basically, we would just define what kind of components we would like to use and select the environment. Here’s an example, we use in production for all our projects:
"""
This is a django-split-settings main file.
For more information read this:
https://github.com/sobolevn/django-split-settings
Default environment is `developement`.
To change settings file:
`DJANGO_ENV=production python manage.py runserver`
"""
from split_settings.tools import optional, include
from os import environ
ENV = environ.get('DJANGO_ENV') or 'development'
base_settings = [
'components/common.py', # standard django settings
'components/database.py', # postgres
'components/rq.py', # redis and redis-queue
'components/emails.py', # smtp
# You can even use glob:
# 'components/*.py'
# Select the right env:
'environments/{0}.py'.format(ENV),
# Optionally override some settings:
optional('environments/local.py'),
]
# Include settings:
include(*base_settings)
And that’s it. Our application would run as usual. We have achieved multiple goals with so few lines of code:
- We now have separated settings based on what they configure. Gaining readability and maintainability
- We now have separated settings based on environment
- We now have optional local settings with now dirty hacks
- We did not have to do any refactoring except just some basic restructuring
We have also created a project example, which can be used as a template for your own projects: https://github.com/wemake-services/wemake-django-template
What’s not covered
In a future articles we would cover two topics which are crucial when dealing with project’s configuration:
- Secret settings
- Dynamic settings
Afterword
Got any ideas or feedback? Dive in, if you want to contribute: django-split-settings - Organize Django settings into multiple files and directories.
Recommend
-
54
You've upgraded to iOS 12, but is it secure enough for the risks of modern life? Here's how to tighten iOS 12 security.
-
23
Django’s default settings file has always included a BASE_DIR pseudo-setting. I call it...
-
8
Django, gestion des settings d'application simplifiéeDjango, gestion des settings d'application simplifiée Fri 29 July 2011Je reprends ici la méthode présentée par
-
17
Tips and Tricks: Managing iOS 13 Bluetooth App Access Settings If you recently upgraded to iOS 13, you may have noticed some new...
-
8
Django modify_settings and receivers Sometimes, tests expose weird behaviour. In this instance, I have a Makefile command that...
-
11
A complete guide to organizing settings in Django Published on November 11th, 2021 in Django/Python How do you organize your Django settings? In this article, we describe a simple approach we refined through many iter...
-
10
Back-end6 minute readStreamline Your Django Settings With Type Hints: A Pydantic TutorialPython 3.5 offers type...
-
6
Managing SharePoint Online tenant settings via the Graph API While the Graph API has a good coverage of “end user” endpoints, which cover most client scenarios, the “admin” part of the experience remains largely non-ex...
-
5
Essential settings to change for a Django app in production Django • Feb 19, 2023 So you have finished your Django app, after mon...
-
4
Windows 11 build 25926 gets a new Settings page for managing system components...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK