请求密钥但是已插入密钥时出错

时间:2016-11-17 02:18:13

标签: python django secret-key

在为Django运行后端时,我一直收到错误。

这是追溯:

    Traceback (most recent call last):
  File "/Users/lyndseearmstrong/dev/recipe_organizer/backend/manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/lyndseearmstrong/.virtualenvs/recipe_organizer/lib/python2.7/site-packages/django/core/management/__init__.py", line 350, in execute_from_command_line
    utility.execute()
  File "/Users/lyndseearmstrong/.virtualenvs/recipe_organizer/lib/python2.7/site-packages/django/core/management/__init__.py", line 342, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/lyndseearmstrong/.virtualenvs/recipe_organizer/lib/python2.7/site-packages/django/core/management/__init__.py", line 195, in fetch_command
    klass = load_command_class(app_name, subcommand)
  File "/Users/lyndseearmstrong/.virtualenvs/recipe_organizer/lib/python2.7/site-packages/django/core/management/__init__.py", line 39, in load_command_class
    module = import_module('%s.management.commands.%s' % (app_name, name))
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/Users/lyndseearmstrong/.virtualenvs/recipe_organizer/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 16, in <module>
    from django.db.migrations.executor import MigrationExecutor
  File "/Users/lyndseearmstrong/.virtualenvs/recipe_organizer/lib/python2.7/site-packages/django/db/migrations/executor.py", line 7, in <module>
    from .loader import MigrationLoader
  File "/Users/lyndseearmstrong/.virtualenvs/recipe_organizer/lib/python2.7/site-packages/django/db/migrations/loader.py", line 10, in <module>
    from django.db.migrations.recorder import MigrationRecorder
  File "/Users/lyndseearmstrong/.virtualenvs/recipe_organizer/lib/python2.7/site-packages/django/db/migrations/recorder.py", line 12, in <module>
    class MigrationRecorder(object):
  File "/Users/lyndseearmstrong/.virtualenvs/recipe_organizer/lib/python2.7/site-packages/django/db/migrations/recorder.py", line 26, in MigrationRecorder
    class Migration(models.Model):
  File "/Users/lyndseearmstrong/.virtualenvs/recipe_organizer/lib/python2.7/site-packages/django/db/migrations/recorder.py", line 27, in Migration
    app = models.CharField(max_length=255)
  File "/Users/lyndseearmstrong/.virtualenvs/recipe_organizer/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 1072, in __init__
    super(CharField, self).__init__(*args, **kwargs)
  File "/Users/lyndseearmstrong/.virtualenvs/recipe_organizer/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 166, in __init__
    self.db_tablespace = db_tablespace or settings.DEFAULT_INDEX_TABLESPACE
  File "/Users/lyndseearmstrong/.virtualenvs/recipe_organizer/lib/python2.7/site-packages/django/conf/__init__.py", line 55, in __getattr__
    self._setup(name)
  File "/Users/lyndseearmstrong/.virtualenvs/recipe_organizer/lib/python2.7/site-packages/django/conf/__init__.py", line 43, in _setup
    self._wrapped = Settings(settings_module)
  File "/Users/lyndseearmstrong/.virtualenvs/recipe_organizer/lib/python2.7/site-packages/django/conf/__init__.py", line 120, in __init__
    raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")
django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.

以下是我的网址:

from django.conf.urls import include, url
from django.contrib import admin
from django.views.static import serve
import settings

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^recipes/', include('apps.recipes.urls')),
    url(r'^media/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT}),
]

还有什么我可以尝试的吗?我知道Django的最新版本存在网址问题,但我并不完全熟悉如何修复它们。

这是我的manage.py文件:

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

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

    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)

此外,这是我的setting.py文件(未插入密钥):

"""
Django settings for recipe_organizer project.

Generated by 'django-admin startproject' using Django 1.8.

For more information on this file, see
https://docs.djangoproject.com/en/1.8/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.8/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '--------------------------------'

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

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'apps.recipes',
    'rest_framework',
    'corsheaders',
)

MIDDLEWARE_CLASSES = (
    'corsheaders.middleware.CorsMiddleware',
    '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',
    'django.middleware.security.SecurityMiddleware',
)

ROOT_URLCONF = 'recipe_organizer.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        '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 = 'recipe_organizer.wsgi.application'


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

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


# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/

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.8/howto/static-files/

MEDIA_ROOT = BASE_DIR + '/apps/recipes/media'

MEDIA_URL = '/media/'

STATIC_ROOT = 'static'

STATIC_URL = '/static/'

CORS_ORIGIN_WHITELIST = (
    'localhost:8000',
    'localhost/',
)


CORS_ORIGIN_ALLOW_ALL = True

1 个答案:

答案 0 :(得分:1)

您的问题是您导入settings而不是django.conf.settings https://docs.djangoproject.com/en/1.10/topics/settings/#using-settings-in-python-code

中使用urls.py
from django.conf.urls import include, url
from django.contrib import admin
from django.views.static import serve
import settings  # <- here

应该是

from django.conf.urls import include, url
from django.contrib import admin
from django.views.static import serve
from django.conf import settings