gpt4 book ai didi

python - Django:即使设置中存在 SECRET_KEY 设置也不能为空

转载 作者:行者123 更新时间:2023-11-30 22:27:36 25 4
gpt4 key购买 nike

我在 windows 7 中使用 python 3.5django 1.11.5

我有包含 base.py、mysql.py 和 sqlite.py 的设置文件夹

manage.py 包含:

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "GuardianAngel.settings")

当我尝试 python manage.py makemigrations 时,出现以下错误:

Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "c:\Python35\lib\site-packages\django\core\management\__init__.py", line 364, in execute_from_command_line
utility.execute()
File "c:\Python35\lib\site-packages\django\core\management\__init__.py", line 356, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "c:\Python35\lib\site-packages\django\core\management\__init__.py", line 206, in fetch_command
klass = load_command_class(app_name, subcommand)
File "c:\Python35\lib\site-packages\django\core\management\__init__.py", line 40, in load_command_class
module = import_module('%s.management.commands.%s' % (app_name, name))
File "c:\Python35\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 662, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "c:\Python35\lib\site-packages\django\core\management\commands\migrate.py", line 15, in <module>
from django.db.migrations.autodetector import MigrationAutodetector
File "c:\Python35\lib\site-packages\django\db\migrations\autodetector.py", line 13, in <module>
from django.db.migrations.questioner import MigrationQuestioner
File "c:\Python35\lib\site-packages\django\db\migrations\questioner.py", line 12, in <module>
from .loader import MigrationLoader
File "c:\Python35\lib\site-packages\django\db\migrations\loader.py", line 10, in <module>
from django.db.migrations.recorder import MigrationRecorder
File "c:\Python35\lib\site-packages\django\db\migrations\recorder.py", line 12, in <module>
class MigrationRecorder(object):
File "c:\Python35\lib\site-packages\django\db\migrations\recorder.py", line 26, in MigrationRecorder
class Migration(models.Model):
File "c:\Python35\lib\site-packages\django\db\migrations\recorder.py", line 27, in Migration
app = models.CharField(max_length=255)
File "c:\Python35\lib\site-packages\django\db\models\fields\__init__.py", line 1061, in __init__
super(CharField, self).__init__(*args, **kwargs)
File "c:\Python35\lib\site-packages\django\db\models\fields\__init__.py", line 172, in __init__
self.db_tablespace = db_tablespace or settings.DEFAULT_INDEX_TABLESPACE
File "c:\Python35\lib\site-packages\django\conf\__init__.py", line 56, in __getattr__
self._setup(name)
File "c:\Python35\lib\site-packages\django\conf\__init__.py", line 41, in _setup
self._wrapped = Settings(settings_module)
File "c:\Python35\lib\site-packages\django\conf\__init__.py", line 129, in __init__
raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")
django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.

如果我将 os.environ.setdefault 更改为 manage.py,如下所示:

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "GuardianAngel.settings.base")

需要注意的是,base.py 包含一个 SECRET_KEY,例如:

SECRET_KEY = 'i$778^0hplfwr_tgm$7p!azz61lkif6qz#-0_*k4o88tq6=7aw'

我没有收到此错误,但收到有关 mysql 的错误。

settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.

你知道如何解决这个问题吗?

最佳答案

设置为:

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "GuardianAngel.settings.base")

但在您的base.py中添加以下内容:

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'DB_NAME',
'USER': 'DB_USERNAME',
'PASSWORD': 'DB_PASSWORD',
'HOST': 'localhost',
'PORT': '3306',
}
}

或者更好的是,在 GuardianAngel 中创建另一个名为 local_settings.py 的文件并添加

from settings.base import *

然后添加上面提到的DATABASES

关于python - Django:即使设置中存在 SECRET_KEY 设置也不能为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46868377/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com