Django:运行migrate命令显示“ SECRET_KEY设置不能为空”错误

时间:2019-02-28 05:55:32

标签: python django

当我运行python manage.py migrate命令时,出现以下堆栈跟踪消息“ SECRET_KEY设置不能为空”错误。但是,我在我的settings \ local.py文件中设置了SECRET_KEY,并且我正在使用py manage.py runserver --insecure --settings=locallibrary.settings.local命令来运行django应用程序。

我缺少什么(希望这不是新手的错误)?

Traceback (most recent call last):
  File "manage.py", line 17, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\AjitGoel\Envs\my_django_environment\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "C:\Users\AjitGoel\Envs\my_django_environment\lib\site-packages\django\core\management\__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\AjitGoel\Envs\my_django_environment\lib\site-packages\django\core\management\__init__.py", line 224, in fetch_command
    klass = load_command_class(app_name, subcommand)
  File "C:\Users\AjitGoel\Envs\my_django_environment\lib\site-packages\django\core\management\__init__.py", line 36, in load_command_class
    module = import_module('%s.management.commands.%s' % (app_name, name))
  File "C:\Users\AjitGoel\Envs\my_django_environment\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\AjitGoel\Envs\my_django_environment\lib\site-packages\django\core\management\commands\makemigrations.py", line 12, in <module>
    from django.db.migrations.autodetector import MigrationAutodetector
  File "C:\Users\AjitGoel\Envs\my_django_environment\lib\site-packages\django\db\migrations\autodetector.py", line 11, in <module>
    from django.db.migrations.questioner import MigrationQuestioner
  File "C:\Users\AjitGoel\Envs\my_django_environment\lib\site-packages\django\db\migrations\questioner.py", line 9, in <module>
    from .loader import MigrationLoader
  File "C:\Users\AjitGoel\Envs\my_django_environment\lib\site-packages\django\db\migrations\loader.py", line 8, in <module>
    from django.db.migrations.recorder import MigrationRecorder
  File "C:\Users\AjitGoel\Envs\my_django_environment\lib\site-packages\django\db\migrations\recorder.py", line 9, in <module>
    class MigrationRecorder:
  File "C:\Users\AjitGoel\Envs\my_django_environment\lib\site-packages\django\db\migrations\recorder.py", line 22, in MigrationRecorder
    class Migration(models.Model):
  File "C:\Users\AjitGoel\Envs\my_django_environment\lib\site-packages\django\db\models\base.py", line 87, in __new__
    app_config = apps.get_containing_app_config(module)
  File "C:\Users\AjitGoel\Envs\my_django_environment\lib\site-packages\django\apps\registry.py", line 249, in get_containing_app_config
    self.check_apps_ready()
  File "C:\Users\AjitGoel\Envs\my_django_environment\lib\site-packages\django\apps\registry.py", line 131, in check_apps_ready
    settings.INSTALLED_APPS
  File "C:\Users\AjitGoel\Envs\my_django_environment\lib\site-packages\django\conf\__init__.py", line 57, in __getattr__
    self._setup(name)
  File "C:\Users\AjitGoel\Envs\my_django_environment\lib\site-packages\django\conf\__init__.py", line 44, in _setup
    self._wrapped = Settings(settings_module)
  File "C:\Users\AjitGoel\Envs\my_django_environment\lib\site-packages\django\conf\__init__.py", line 126, in __init__
    raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")
django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.

locallibrary \ settings \ local.py:

from locallibrary.settings.base import *
DEBUG = True
SECRET_KEY = '<some secret key>'
ALLOWED_HOSTS = ['127.0.0.1']
SECURE_SSL_REDIRECT = False
INSTALLED_APPS += (
    #'debug_toolbar',
)
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

locallibrary \ settings \ base.py:

import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'catalog.apps.CatalogConfig', 
    'crispy_forms',
]
CRISPY_TEMPLATE_PACK = 'bootstrap4'
MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'locallibrary.urls'
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(BASE_DIR, 'templates'),
            ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]
WSGI_APPLICATION = 'locallibrary.wsgi.application'
AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'America/Chicago'
USE_I18N = True
USE_L10N = True
USE_TZ = True
LOGIN_REDIRECT_URL = '/'
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
STRIPE_PUBLISHABLE_KEY='<SomeSTRIPE_PUBLISHABLE_KEY>'
STRIPE_SECRET_KEY='<SomeSecretKey>'
EMAIL_HOST = 'smtp.mailgun.org'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'postmaster@mg.bottlenose.co'
EMAIL_HOST_PASSWORD = '<SomePassword>'
EMAIL_USE_TLS = True
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

manage.py:

#!/usr/bin/env python
import os
import sys
if __name__ == '__main__':
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'locallibrary.settings')    
    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH environment variable? Did you "
            "forget to activate a virtual environment?"
        ) from exc
    execute_from_command_line(sys.argv)

2 个答案:

答案 0 :(得分:0)

将空白__init__.py文件添加到locallibrary\settings路径

然后通过python manage.py runserver --insecure --settings=locallibrary.settings

运行管理命令

答案 1 :(得分:-1)

我能够通过将wsgi.py和manage.py文件更改为将'DJANGO_SETTINGS_MODULE'指向'locallibrary.settings.local'而不是'locallibrary.settings'来解决这个问题