2016-11-17 65 views
0

運行我的後端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 
+0

您使用正確的設置文件?你與SECRET_KEY的環境變量引用一個是settings.py,但你的manage.py載荷'os.environ.setdefault( 「DJANGO_SETTINGS_MODULE」, 「recipe_organizer.settings」)' –

+0

[這可能有助於](HTTP:// stackoverflow.com/questions/19128540/django-improperlyconfigured-the-secret-key-setting-must-not-be-empty)。基本上,循環依賴意義是可能的。 – Aditya

+0

你知道,要解決這個循環依賴是什麼?我知道這可能是一些事情,但我找不到它。 –

回答

1

您的問題是,您要導入的settings而不是在你的urls.pyhttps://docs.djangoproject.com/en/1.10/topics/settings/#using-settings-in-python-code

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 
+0

有問題。萬分感謝。 –

+0

@Lynds歡迎您。如果它解決了您的問題,請不要忘記接受答案。 –