A
A
Antigo_ptz2017-03-03 16:36:58
Django
Antigo_ptz, 2017-03-03 16:36:58

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'),
        },
    }
 ...
 ...

file /opt/settings/settings.ini
[SYSTEM]

DATABASE_NAME = 'db'
DATABASE_USER = 'admin'
DATABASE_PASSWORD = '12345678'
DATABASE_HOST = '192.168.1.22'
DATABASE_PORT = '5432'

The question is, how will this affect the operation of the system? How does Django itself work with settings, read it once and use it, or does it constantly knock on settings.py as needed?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman Kitaev, 2017-03-03
@Antigo_ptz

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.

S
sim3x, 2017-03-03
@sim3x

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

add to
.bashrc
from os import environ
# ...
SECRET_KEY = environ('SECRET_KEY')
# ...

Changes to the file will not be picked up by the machine - you will need to restart uWSGI

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question