M
M
mkone1122020-10-31 00:05:00
Django
mkone112, 2020-10-31 00:05:00

How to store django secrets in dynaconf?

I want to move SECRET_KEY out of the repository - stumbled upon dynaconf. Do I understand correctly that you need to remove SECRET_KEY from settings.py and move it to .secrets.toml?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2020-10-31
@pyHammer

mkone112 tell me how I do it.
To begin with, I will explain what local_settings.py is, it is a file with local settings where you can override any global setting (including SECRET_KEY). Its content looks something like this

from .settings import *

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False

This file must be added to .gitignore and created manually or automatically on the server where the deployment is performed. The most important thing is that this file does not get into the repository.
Thus, you don’t have to worry about SECRET_KEY that will be sent to the repository, you just redefine it to an arbitrary one during deployment, like this
from .settings import *

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'Мой самый секретный ключ'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False

Next, you use the settings file at startup like this, during development. In gunicorn, respectively, you need to add the same setting.
$ python manage.py runserver --settings=myproject.local_settings

Thus your problem is solved. And your SECRET_KEY in the repository will have nothing to do with the real SECRET_KEY on the production server.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question