我正在使用python-decouple 3.4为我的 django 应用程序设置环境变量。我的.env
文件与manage.py
. 除了 SECRET_KEY(在 settings.py 中),在其中一个settings.py
或views.py
直接加载其他环境变量失败,说明它们尚未定义。其他给出错误的环境变量将用于views.py
.
这是我的.env
文件:-
SECRET_KEY=<django_app_secret_key>
file_path=<path_to_the file>
如果我尝试将它们定义为settings.py
:-
from decouple import config
FILE_PATH = config('file_path')
然后在 中使用views.py
它们
from django.conf.settings import FILE_PATH
print(FILE_PATH)
然后我也得到同样的错误。如何为我的views.py
具体定义环境变量?
[编辑:这是我得到的错误:-
raise UndefinedValueError('{} not found. Declare it as envvar or define a default value.'.format(option))
decouple.UndefinedValueError: file_path not found. Declare it as envvar or define a default value.
我是否用过这个
from decouple import config
FILE_PATH = config('file_path')
直接输入settings.py
或views.py
直接输入或先输入settings.py
再输入,views.py
如上例所示]