python-解耦 | SECRET_KEY 设置不能为空
python-decouple | The SECRET_KEY setting must not be empty
该项目是使用 Django 3.1.1 创建的。最近将其更新为 Django 3.1.4,并且运行良好。之后安装了python-decouple 3.3,也正常。我们删除了我们的测试数据库(故意)以查看是否一切正常并且发生了这种情况。当运行 python manage.py makemigrations
出现以下错误
Traceback (most recent call last):
File "manage.py", line 22, in <module>
main()
File "manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "P:\crm\env\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
utility.execute()
File "P:\crm\env\lib\site-packages\django\core\management\__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "P:\crm\env\lib\site-packages\django\core\management\base.py", line 330, in run_from_argv
self.execute(*args, **cmd_options)
File "P:\crm\env\lib\site-packages\django\core\management\base.py", line 368, in execute
self.check()
File "P:\crm\env\lib\site-packages\django\core\management\base.py", line 392, in check
all_issues = checks.run_checks(
File "P:\crm\env\lib\site-packages\django\core\checks\registry.py", line 70, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "P:\crm\env\lib\site-packages\django\core\checks\templates.py", line 22, in
check_setting_app_dirs_loaders
for conf in settings.TEMPLATES
File "P:\crm\env\lib\site-packages\django\conf\__init__.py", line 83, in __getattr__
self._setup(name)
File "P:\crm\env\lib\site-packages\django\conf\__init__.py", line 70, in _setup
self._wrapped = Settings(settings_module)
File "P:\crm\env\lib\site-packages\django\conf\__init__.py", line 177, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "C:\Users\rodri\AppData\Local\Programs\Python\Python38\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 "P:\crm\app\config\settings.py", line 13, in <module>
from core.user.models import User as u
File "P:\crm\app\core\user\models.py", line 1, in <module>
from django.contrib.auth.base_user import AbstractBaseUser
File "P:\crm\env\lib\site-packages\django\contrib\auth\base_user.py", line 48, in <module>
class AbstractBaseUser(models.Model):
File "P:\crm\env\lib\site-packages\django\db\models\base.py", line 108, in __new__
app_config = apps.get_containing_app_config(module)
File "P:\crm\env\lib\site-packages\django\apps\registry.py", line 253, in get_containing_app_config
self.check_apps_ready()
File "P:\crm\env\lib\site-packages\django\apps\registry.py", line 135, in check_apps_ready
settings.INSTALLED_APPS
File "P:\crm\env\lib\site-packages\django\conf\__init__.py", line 83, in __getattr__
self._setup(name)
File "P:\crm\env\lib\site-packages\django\conf\__init__.py", line 70, in _setup
self._wrapped = Settings(settings_module)
File "P:\crm\env\lib\site-packages\django\conf\__init__.py", line 196, in __init__
raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")
django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.
我的项目结构如下:
project
|
|- config
| |- .env
| |- settings.py
|- core
|- app1
|- app2
这是我的settings.py
import config.db as db
from core.user.models import User # Custom user model
from decouple import config
from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
SECRET_KEY = config('SECRET_KEY')
DEBUG = config('DEBUG', cast=bool)
...
这是.env
文件结构
SECRET_KEY=abcdefghikjlmnopqrstuvwxyz
DEBUG=True
...
我已经尝试了此处建议的许多备选方案,但其中 none 行之有效。
从python解耦docs:
Simply create a .env text file on your repository's root directory in the form
因此您需要将 .env
文件放入项目文件夹(与 manage.py
.
相同的文件夹
(@saquintes 关于在 settings.py
中导入模型的说法也是正确的。我怀疑您对 AUTH_USER_MODEL
设置很重要。您应该使用字符串,而不是此设置的实际模型. 见 here.)
问题是您无法在设置完成之前导入模型。因此,当您在设置文件的顶部导入模型时,您会收到错误消息,因为设置对象尚未初始化,因此没有设置 SECRET_KEY 字段。
理想情况下,您根本不需要在设置中导入模型。但是如果你必须把它们放在那里,你可以简单地将它们移动到比 SECRET_KEY 晚的位置,但是......它可能仍然会导致问题。我会尝试重构,这样您的设置文件中就不需要它们了。
该项目是使用 Django 3.1.1 创建的。最近将其更新为 Django 3.1.4,并且运行良好。之后安装了python-decouple 3.3,也正常。我们删除了我们的测试数据库(故意)以查看是否一切正常并且发生了这种情况。当运行 python manage.py makemigrations
出现以下错误
Traceback (most recent call last):
File "manage.py", line 22, in <module>
main()
File "manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "P:\crm\env\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
utility.execute()
File "P:\crm\env\lib\site-packages\django\core\management\__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "P:\crm\env\lib\site-packages\django\core\management\base.py", line 330, in run_from_argv
self.execute(*args, **cmd_options)
File "P:\crm\env\lib\site-packages\django\core\management\base.py", line 368, in execute
self.check()
File "P:\crm\env\lib\site-packages\django\core\management\base.py", line 392, in check
all_issues = checks.run_checks(
File "P:\crm\env\lib\site-packages\django\core\checks\registry.py", line 70, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "P:\crm\env\lib\site-packages\django\core\checks\templates.py", line 22, in
check_setting_app_dirs_loaders
for conf in settings.TEMPLATES
File "P:\crm\env\lib\site-packages\django\conf\__init__.py", line 83, in __getattr__
self._setup(name)
File "P:\crm\env\lib\site-packages\django\conf\__init__.py", line 70, in _setup
self._wrapped = Settings(settings_module)
File "P:\crm\env\lib\site-packages\django\conf\__init__.py", line 177, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "C:\Users\rodri\AppData\Local\Programs\Python\Python38\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 "P:\crm\app\config\settings.py", line 13, in <module>
from core.user.models import User as u
File "P:\crm\app\core\user\models.py", line 1, in <module>
from django.contrib.auth.base_user import AbstractBaseUser
File "P:\crm\env\lib\site-packages\django\contrib\auth\base_user.py", line 48, in <module>
class AbstractBaseUser(models.Model):
File "P:\crm\env\lib\site-packages\django\db\models\base.py", line 108, in __new__
app_config = apps.get_containing_app_config(module)
File "P:\crm\env\lib\site-packages\django\apps\registry.py", line 253, in get_containing_app_config
self.check_apps_ready()
File "P:\crm\env\lib\site-packages\django\apps\registry.py", line 135, in check_apps_ready
settings.INSTALLED_APPS
File "P:\crm\env\lib\site-packages\django\conf\__init__.py", line 83, in __getattr__
self._setup(name)
File "P:\crm\env\lib\site-packages\django\conf\__init__.py", line 70, in _setup
self._wrapped = Settings(settings_module)
File "P:\crm\env\lib\site-packages\django\conf\__init__.py", line 196, in __init__
raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")
django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.
我的项目结构如下:
project
|
|- config
| |- .env
| |- settings.py
|- core
|- app1
|- app2
这是我的settings.py
import config.db as db
from core.user.models import User # Custom user model
from decouple import config
from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
SECRET_KEY = config('SECRET_KEY')
DEBUG = config('DEBUG', cast=bool)
...
这是.env
文件结构
SECRET_KEY=abcdefghikjlmnopqrstuvwxyz
DEBUG=True
...
我已经尝试了此处建议的许多备选方案,但其中 none 行之有效。
从python解耦docs:
Simply create a .env text file on your repository's root directory in the form
因此您需要将 .env
文件放入项目文件夹(与 manage.py
.
(@saquintes 关于在 settings.py
中导入模型的说法也是正确的。我怀疑您对 AUTH_USER_MODEL
设置很重要。您应该使用字符串,而不是此设置的实际模型. 见 here.)
问题是您无法在设置完成之前导入模型。因此,当您在设置文件的顶部导入模型时,您会收到错误消息,因为设置对象尚未初始化,因此没有设置 SECRET_KEY 字段。
理想情况下,您根本不需要在设置中导入模型。但是如果你必须把它们放在那里,你可以简单地将它们移动到比 SECRET_KEY 晚的位置,但是......它可能仍然会导致问题。我会尝试重构,这样您的设置文件中就不需要它们了。