Django:即使SECRET_KEY设置存在于设置中,它也不能为空

时间:2017-10-21 21:39:30

标签: python django python-3.x

我在python 3.5中使用django 1.11.5windows 7

我的设置文件夹包含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.

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

1 个答案:

答案 0 :(得分:3)

将其设为:

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