这个项目工作正常,直到我使用environ使SECRET_KEY和DEBUG作为环境变量使用environ.在我得到这个错误:-
输出为:
(env) E:\ecommercedj>python manage.py runserver
Traceback (most recent call last):
File "E:\ecommercedj\env\lib\site-packages\environ\environ.py", line 273, in get_value
value = self.ENVIRON[var]
File "c:\users\matruchhaya\appdata\local\programs\python\python38-32\lib\os.py", line 675, in __getitem__
raise KeyError(key) from None
KeyError: 'SECRET_KEY'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "E:\ecommercedj\env\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
utility.execute()
File "E:\ecommercedj\env\lib\site-packages\django\core\management\__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "E:\ecommercedj\env\lib\site-packages\django\core\management\base.py", line 328, in run_from_argv
self.execute(*args, **cmd_options)
File "E:\ecommercedj\env\lib\site-packages\django\core\management\commands\runserver.py", line 60, in execute
super().execute(*args, **options)
File "E:\ecommercedj\env\lib\site-packages\django\core\management\base.py", line 369, in execute
output = self.handle(*args, **options)
File "E:\ecommercedj\env\lib\site-packages\django\core\management\commands\runserver.py", line 67, in handle
if not settings.DEBUG and not settings.ALLOWED_HOSTS:
File "E:\ecommercedj\env\lib\site-packages\django\conf\__init__.py", line 76, in __getattr__
self._setup(name)
File "E:\ecommercedj\env\lib\site-packages\django\conf\__init__.py", line 63, in _setup
self._wrapped = Settings(settings_module)
File "E:\ecommercedj\env\lib\site-packages\django\conf\__init__.py", line 142, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "c:\users\matruchhaya\appdata\local\programs\python\python38-32\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "E:\ecommercedj\ecom\settings.py", line 11, in <module>
SECRET_KEY = env('SECRET_KEY')
File "E:\ecommercedj\env\lib\site-packages\environ\environ.py", line 123, in __call__
return self.get_value(var, cast=cast, default=default, parse_default=parse_default)
File "E:\ecommercedj\env\lib\site-packages\environ\environ.py", line 277, in get_value
raise ImproperlyConfigured(error_msg)
django.core.exceptions.ImproperlyConfigured: Set the SECRET_KEY environment variable
(env) E:\ecommercedj>
settings.py
import os
import environ
env = environ.Env()
# read th .env file
environ.Env.read_env()
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SECRET_KEY = env('SECRET_KEY')
DEBUG = env('DEBUG')
ALLOWED_HOSTS = ['*']
.....
.env文件
SECRET_KEY值未加引号
SECRET_KEY = my_secret_key_value
DEBUG = True
在添加environ之前,项目运行良好。我得到这个错误是因为environ吗?我应该如何删除这个错误?
9条答案
按热度按时间5jdjgkvh1#
解决了!!
在.env文件中删除赋值运算符和var之间的空格,以及值和赋值运算符之间的空格。
8aqjt8rx2#
.env
文件应与settings.py
位于同一目录中0x6upsns3#
1.首先在设置所在的目录中创建.env文件
1.其次,删除变量和键值之间的所有空格(即
KEY=my_key
)ou6hu8tu4#
首先,像这样安装Django-environ
接下来,像这样在www.example.com中导入环境settings.py
这是一个普通的Django设置文件,你所做的就是从异常中导入os,environ,ImproprilyConfigured,编写函数get_env_variable并根据SECRET_KEY调用它
接下来,在与包含此内容的www.example.com文件相同的目录中创建一个.env文件settings.py
用你自己的. you can reference this material替换密钥字符串
o3imoua45#
在SECRET_KEY=my_secret_key_value之间没有空格为我解决了这个问题!
vsikbqxv6#
清除.env中的空格!!
在.env文件中删除赋值运算符和var之间的空格,以及值和赋值运算符之间的空格。
bvjveswy7#
如果您的
SECRET_KEY
以$
(美元符号)开头,您将遇到与OP相同的异常,这是一个已知的bug,带有django-environ
包。在这种情况下,只需生成新的SECRET_KEY:
ejk8hzay8#
其实有一个简单的解决办法。
settings.py
.env文件
最后,不要在.env文件中的等号前后使用空格。
pdkcd3nj9#
也有同样的错误。我个人的问题是由于
.env
文件的位置。我的文件结构如下:
所以我的env应用程序和我的Django-project文件夹在同一层
在我的www.example.com中settings.py,在声明BASE_DIR之后,我包括:
env.read_env('../.env')
我猜这基本上是将文件夹从
BASE_DIR
向上发送到它需要读取的.env的位置。