python - django.core.exceptions.ImproperlyConfigured : The SECRET_KEY setting must not be empty

标签 python django

我在 Django 中创建了一个新项目并粘贴了另一个项目中的一些文件。每当我尝试运行服务器时,都会收到以下错误消息:

    Traceback (most recent call last):
      File "manage.py", line 10, in <module>
        execute_from_command_line(sys.argv)
      File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
        utility.execute()
      .
      .
      .
            File "/Library/Python/2.7/site-packages/django/conf/__init__.py", line 115, in __init__
        raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")
    django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.

这是我的设置.py

""" Django settings for dbe project.  """

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
from os.path import join as pjoin
BASE_DIR = os.path.dirname(os.path.dirname(__file__))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/


# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
TEMPLATE_DEBUG = True
ALLOWED_HOSTS = []

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'shared',
    'issues',
    'blog',
    'bombquiz',
    'forum',
    'portfolio',
    'questionnaire',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

TEMPLATE_DIRS = (
     os.path.join(BASE_DIR, "templates"),
     os.path.join(BASE_DIR, "templates", "issues"),
     os.path.join(BASE_DIR, "templates", "blog"),
     os.path.join(BASE_DIR, "templates", "bombquiz"),
     os.path.join(BASE_DIR, "templates", "forum"),
     os.path.join(BASE_DIR, "templates", "portfolio"),
     os.path.join(BASE_DIR, "templates", "questionnaire"),
     )

ROOT_URLCONF = 'dbe.urls'

WSGI_APPLICATION = 'dbe.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/

MEDIA_URL = '/media/'
MEDIA_ROOT = pjoin(BASE_DIR, "media")
STATIC_URL = '/static/'
STATICFILES_DIRS = (
                    pjoin(BASE_DIR, "static"),
                    )

try:
    from local_settings import *
except:
    pass

这里还有manage.py

#!/usr/bin/env python
import os
import sys

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "dbe.settings")

    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)

有什么帮助吗?谢谢!

最佳答案

正如错误所说,您没有定义SECRET_KEY。您需要将一个添加到您的 settings.py

Django will refuse to start if SECRET_KEY is not set.

您可以阅读有关此设置 in the docs 的更多信息。

SECRET_KEY 可以是任何东西……但是如果你想使用 Django 生成一个,你可以从 python shell 中执行以下操作:

>>> from django.utils.crypto import get_random_string
>>> chars = 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)'
>>> SECRET_KEY = get_random_string(50, chars)
>>> print SECRET_KEY

SECRET_KEY 复制到您的设置文件。

关于python - django.core.exceptions.ImproperlyConfigured : The SECRET_KEY setting must not be empty,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29455057/

相关文章:

python - 类型错误 : unsupported format string passed to Tensor. __format__

python - 从 LDAP 获取组到 django

python - Django Postgres多个架构迁移:“无迁移适用”

django - 从Django View 中写入 Elasticsearch

django - 在 Django 中动态更改 QuerySet 对象

python - 无法并排绘制seaborn图表

python - 使用 Seaborn 的分布图的部分阴影

python - Emacs:在缓冲区评估期间将参数传递给劣质 Python shell

python - 字符串出现次数计数算法

django - 迁移新用户模型时出错