django.core.exceptions.ImproperlyConfigured:设置上一个问题中所说的 SECRET_KEY 环境变量不起作用

问题描述 投票:0回答:1

我无法运行该项目,我按照 README.md 中的指示进行了操作,但仍然出现错误。

输出是:

Traceback (most recent call last):
  File "C:\Users\user\AppData\Roaming\Python\Python311\site-packages\environ\environ.py", line 403, in get_value
    value = self.ENVIRON[var_name]
            ~~~~~~~~~~~~^^^^^^^^^^
  File "<frozen os>", line 678, in __getitem__
KeyError: 'SECRET_KEY'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\new-folder\manage.py", line 21, in <module>
    main()
  File "C:\new-folder\manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\user\AppData\Roaming\Python\Python311\site-packages\django\core\management\__init__.py", line 446, in execute_from_command_line
    utility.execute()
  File "C:\Users\user\AppData\Roaming\Python\Python311\site-packages\django\core\management\__init__.py", line 440, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\user\AppData\Roaming\Python\Python311\site-packages\django\core\management\base.py", line 402, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\user\AppData\Roaming\Python\Python311\site-packages\django\core\management\base.py", line 448, in execute
    output = self.handle(*args, **options)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user\AppData\Roaming\Python\Python311\site-packages\django\core\management\base.py", line 93, in wrapped
    saved_locale = translation.get_language()
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user\AppData\Roaming\Python\Python311\site-packages\django\utils\translation\__init__.py", line 210, in get_language
    return _trans.get_language()
           ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user\AppData\Roaming\Python\Python311\site-packages\django\utils\translation\__init__.py", line 65, in __getattr__
    if settings.USE_I18N:
       ^^^^^^^^^^^^^^^^^
  File "C:\Users\user\AppData\Roaming\Python\Python311\site-packages\django\conf\__init__.py", line 92, in __getattr__
    self._setup(name)
  File "C:\Users\user\AppData\Roaming\Python\Python311\site-packages\django\conf\__init__.py", line 79, in _setup
    self._wrapped = Settings(settings_module)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user\AppData\Roaming\Python\Python311\site-packages\django\conf\__init__.py", line 190, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python311\Lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1206, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1178, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1149, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 940, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "C:\new-folder\usn_uchet\settings.py", line 26, in <module>
    SECRET_KEY = ENV.str('SECRET_KEY')
                 ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user\AppData\Roaming\Python\Python311\site-packages\environ\environ.py", line 213, in str
    value = self.get_value(var, cast=str, default=default)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user\AppData\Roaming\Python\Python311\site-packages\environ\environ.py", line 407, in get_value
    raise ImproperlyConfigured(error_msg) from exc
django.core.exceptions.ImproperlyConfigured: Set the SECRET_KEY environment variable

settings.py 文件

import os
import environ
from pathlib import Path
from django.utils.log import DEFAULT_LOGGING
import urllib3

import sentry_sdk
from sentry_sdk.integrations.django import DjangoIntegration


BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

ENV = environ.Env(DEBUG=(bool, True))


ENV_FILE = Path(os.path.join(BASE_DIR, '.env'))
if ENV_FILE.exists():
    environ.Env.read_env(os.path.join(BASE_DIR, '.env'))

DEBUG = ENV.bool('DEBUG', True)
LOGLEVEL = ENV.str('LOGLEVEL', 'INFO')
SECRET_KEY = ENV.str('SECRET_KEY')
CRYPTO_KEY = bytes(ENV.str('CRYPTO_KEY'), 'UTF-8')
ALLOWED_HOSTS = ENV.list('ALLOWED_HOSTS', default=[])

.env 文件

DEBUG=on
SECRET_KEY=My_key

如何修复此错误以启动服务器? 如何消除此错误尝试删除空格,同时添加到 settings.py 的目录中。您在上一个问题中所说的不起作用

python django environment-variables secret-key environ
1个回答
0
投票

在 Windows 中打开 CMD,然后写入以下命令 setx /m

重启 IDE 并重新运行 Django 项目后,错误就会消失。

© www.soinside.com 2019 - 2024. All rights reserved.