我对Django非常python-decouple陌生,并尝试配置为使用.env变量。我DB_PASSWORD not found. Declare it as envvar or define a default value.在尝试运行服务器时遇到问题。.env文件位于根目录中。这是我的代码:
python-decouple
DB_PASSWORD not found. Declare it as envvar or define a default value.
settings.py
import os from decouple import config DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': 'db_development', 'USER': "db_user", 'PASSWORD': config('DB_PASSWORD'), } }
my_app.env
DB_PASSWORD=my_password
更改文件名:my_app.env必须为.env。
.env
从source代码:
source
class AutoConfig(object): """ Autodetects the config file and type. """ SUPPORTED = { 'settings.ini': RepositoryIni, '.env': RepositoryEnv, }