Answer the question
In order to leave comments, you need to log in
Django settings?
Guys, the question is this.
The Django project has a settings.py file. I want to make the settings more readable, like this:
DATABASE_NAME = 'db'
DATABASE_HOST = '192.168.1.22', etc.
, and move them to the settings.ini file outside the project, and read them from settings.ini in the settings.py file using Python standard library configparser https://docs.python.org/3/library/configparser.html
As a result, the settings.py file will have something like this:
import configparser
config = configparser.ConfigParser()
config.read('/opt/settings/settings.ini')
system = config['SYSTEM']
DATABASES = {
'default': {
'NAME': config.get('SYSTEM', 'DATABASE_NAME'),
'ENGINE': 'django.db.backends.postgresql',
'USER': config.get('SYSTEM', 'DATABASE_USER'),
'PASSWORD': config.get('SYSTEM', 'DATABASE_PASSWORD'),
'HOST': config.get('SYSTEM', 'DATABASE_HOST'),
'PORT': config.get('SYSTEM', 'DATABASE_PORT'),
},
}
...
...
[SYSTEM]
DATABASE_NAME = 'db'
DATABASE_USER = 'admin'
DATABASE_PASSWORD = '12345678'
DATABASE_HOST = '192.168.1.22'
DATABASE_PORT = '5432'
Answer the question
In order to leave comments, you need to log in
All constants are kept in RAM. I don’t understand why complicate your life and create another layer with settings. Yes, it's lousy that dzhanga stores the config with code, but this is dzhanga. And either create a universal reusable app, or keep it like everyone else. While it looks like a huge crutch.
There are two dimensions of problems
The first one has already been described by Roman - there are several types of settings - prod, test, dev
They can have differences
All settings are stored in the repository
Those text is open
This is bad and not safe
One of the solutions is environment variables
stackoverflow.com/a/20909045
We take .env file
$ cat ~/.env
DATABASE_NAME=db
DATABASE_USER=admin
DATABASE_PASSWORD=12345678
DATABASE_HOST=192.168.1.22
DATABASE_PORT=5432
SECRET_KEY=SECRET_KEY
from os import environ
# ...
SECRET_KEY = environ('SECRET_KEY')
# ...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question