SECRET_KEY设置不能为空||可在Settings.py上找到

时间:2014-05-04 14:56:13

标签: python django

我试图找到这个错误,但不知道如何解决它。

我不断收到错误消息“SECRET_KEY设置不能为空。”执行populate_rango.py

我已经检查了settings.py,并且已经插入了密钥。

以下是完整的错误消息:

E:\PythonCode\django1\tango_with_django>python populate_rango.py
Starting Rango population script...
Traceback (most recent call last):
  File "populate_rango.py", line 27, in <module>
    from rango.models import Category, Page
  File "E:\PythonCode\django1\tango_with_django\rango\models.py", line 1, in <mo
dule>
    from django.db import models
  File "C:\python27\lib\site-packages\django\db\models\__init__.py", line 5, in
<module>
    from django.db.models.query import Q
  File "C:\python27\lib\site-packages\django\db\models\query.py", line 17, in <m
odule>
    from django.db.models.deletion import Collector
  File "C:\python27\lib\site-packages\django\db\models\deletion.py", line 4, in
<module>
    from django.db.models import signals, sql
  File "C:\python27\lib\site-packages\django\db\models\sql\__init__.py", line 4,
 in <module>
    from django.db.models.sql.subqueries import *
  File "C:\python27\lib\site-packages\django\db\models\sql\subqueries.py", line
12, in <module>
    from django.db.models.sql.query import Query
  File "C:\python27\lib\site-packages\django\db\models\sql\query.py", line 22, i
n <module>
    from django.db.models.sql import aggregates as base_aggregates_module
  File "C:\python27\lib\site-packages\django\db\models\sql\aggregates.py", line
9, in <module>
    ordinal_aggregate_field = IntegerField()
  File "C:\python27\lib\site-packages\django\db\models\fields\__init__.py", line
 116, in __init__
    self.db_tablespace = db_tablespace or settings.DEFAULT_INDEX_TABLESPACE
  File "C:\python27\lib\site-packages\django\conf\__init__.py", line 54, in __ge
tattr__
    self._setup(name)
  File "C:\python27\lib\site-packages\django\conf\__init__.py", line 49, in _set
up
    self._wrapped = Settings(settings_module)
  File "C:\python27\lib\site-packages\django\conf\__init__.py", line 151, in __i
nit__
    raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")
django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be
empty.

E:\PythonCode\django1\tango_with_django>python populate_rango.py
Starting Rango population script...
Traceback (most recent call last):
  File "populate_rango.py", line 27, in <module>
    from rango.models import Category, Page
  File "E:\PythonCode\django1\tango_with_django\rango\models.py", line 1, in <mo
dule>
    from django.db import models
  File "C:\python27\lib\site-packages\django\db\models\__init__.py", line 5, in
<module>
    from django.db.models.query import Q
  File "C:\python27\lib\site-packages\django\db\models\query.py", line 17, in <m
odule>
    from django.db.models.deletion import Collector
  File "C:\python27\lib\site-packages\django\db\models\deletion.py", line 4, in
<module>
    from django.db.models import signals, sql
  File "C:\python27\lib\site-packages\django\db\models\sql\__init__.py", line 4,
 in <module>
    from django.db.models.sql.subqueries import *
  File "C:\python27\lib\site-packages\django\db\models\sql\subqueries.py", line
12, in <module>
    from django.db.models.sql.query import Query
  File "C:\python27\lib\site-packages\django\db\models\sql\query.py", line 22, i
n <module>
    from django.db.models.sql import aggregates as base_aggregates_module
  File "C:\python27\lib\site-packages\django\db\models\sql\aggregates.py", line
9, in <module>
    ordinal_aggregate_field = IntegerField()
  File "C:\python27\lib\site-packages\django\db\models\fields\__init__.py", line
 116, in __init__
    self.db_tablespace = db_tablespace or settings.DEFAULT_INDEX_TABLESPACE
  File "C:\python27\lib\site-packages\django\conf\__init__.py", line 54, in __ge
tattr__
    self._setup(name)
  File "C:\python27\lib\site-packages\django\conf\__init__.py", line 49, in _set
up
    self._wrapped = Settings(settings_module)
  File "C:\python27\lib\site-packages\django\conf\__init__.py", line 151, in __i
nit__
    raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")
django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be
empty.

这是settings.py:

"""
Django settings for tango_with_django project.

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

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

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

TEMPLATE_PATH = os.path.join(BASE_DIR, 'templates')

TEMPLATE_DIRS = (
    TEMPLATE_PATH,
    )

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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'y130-j9oz4r5aoamn_n=+s-*7n)*3^s$jmf4(qw6ik28()g^(n'

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

TEMPLATE_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',
    'rango',
)

MIDDLEWARE_CLASSES = (
    '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 = 'tango_with_django.urls'

WSGI_APPLICATION = 'tango_with_django.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.6/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.6/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.6/howto/static-files/

STATIC_URL = '/static/'

STATIC_PATH = os.path.join(BASE_DIR, 'static')

STATICFILES_DIRS = (
    STATIC_PATH,
)

这是populate_rango.py:

import os

def populate():
    python_cat = add_cat('Python')

    add_page(cat=python_cat,
        title="Official Python Tutorial",
        url="http://docs.python.org/2/tutorial/")

    for c in Category.objects.all():
        for p in Page.objects.filter(category=c):
            print "- {0} - {1}".format(str(c),str(p))


def add_page(cat, title, url, views=0):
    p = Page.objects.get_or_create(category=cat, title=title, url=url, views=views)[0]
    return p

def add_cat(name):
    c = Category.objects.get_or_create(name=name)[0]
    return c

#start execution here
if __name__=='__main__':
    print "Starting Rango population script..."
    os.environ.setdefault('DJANGO_SETTINGS_MODULE','tango_with_django')
    from rango.models import Category, Page
    populate()

3 个答案:

答案 0 :(得分:9)

如果您可以发布项目的目录结构(如@knbk所述),我们可以帮助解决更多问题。假设项目名称是'tango_with_django'并且大部分设置是本地和默认的,那么这个修改应该修复它:

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'tango_with_django.settings')

如果你正在运行'python manage.py runserver'而不是'python populate_rango.py',那么请确保在每个文件中都有以下代码:

<强> manage.py

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "tango_with_django.settings")

wsgi.py(用于服务器设置)

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "tango_with_django.settings")

答案 1 :(得分:3)

    #The SECRET_KEY setting must not be empty ??
    # I uses Django-oscar framework  and
    #Traceback (most recent call last):
    File "C:\Users\sdhaka\workspace\suresh\src\manage.py", line 10, in <module>
     execute_from_command_line(sys.argv)
    File "C:\Python34\lib\site-packages\django\core\management\__init__.py", line 399, in       execute_from_command_line
    utility.execute()
    File "C:\Python34\lib\site-packages\django\core\management\__init__.py", line 392, in execute
     self.fetch_command(subcommand).run_from_argv(self.argv)
     File "C:\Python34\lib\site-packages\django\core\management\base.py", line 242, in run_from_argv
    self.execute(*args, **options.__dict__)
   File "C:\Python34\lib\site-packages\django\core\management\base.py", line 279, in execute
    saved_locale = translation.get_language()
    File "C:\Python34\lib\site-packages\django\utils\translation\__init__.py", line 154, in get_language
    return _trans.get_language()
    File "C:\Python34\lib\site-packages\django\utils\translation\__init__.py", line 52, in __getattr__
    if settings.USE_I18N:
     self._setup(name)
   File "C:\Python34\lib\site-packages\django\conf\__init__.py", line 49, in _setup
      self._wrapped = Settings(settings_module)
  File "C:\Python34\lib\site-packages\django\conf\__init__.py", line 128, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "C:\Python34\lib\importlib\__init__.py", line 104, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 2231, in _gcd_import
  File "<frozen importlib._bootstrap>", line 2214, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2203, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1129, in _exec
  File "<frozen importlib._bootstrap>", line 1448, in exec_module
  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
  File "C:\Users\sdhaka\workspace\suresh\src\suresh\settings.py", line 14, in <module>
    from oscar import get_core_apps
  File "C:\Python34\oscar.py", line 11, in <module>
    from django.contrib.staticfiles.finders import find
  File "C:\Python34\lib\site-packages\django\contrib\staticfiles\finders.py", line 12, in   <module>
    from django.contrib.staticfiles.storage import AppStaticStorage
    File "C:\Python34\lib\site-packages\django\contrib\staticfiles\storage.py", line 8, in    <module>
    from django.core.cache import (get_cache, InvalidCacheBackendError,
  File "C:\Python34\lib\site-packages\django\core\cache\__init__.py", line 69, in <module>
    if DEFAULT_CACHE_ALIAS not in settings.CACHES:
  File "C:\Python34\lib\site-packages\django\conf\__init__.py", line 54, in __getattr__
    self._setup(name)
  File "C:\Python34\lib\site-packages\django\conf\__init__.py", line 49, in _setup
    self._wrapped = Settings(settings_module)
  File "C:\Python34\lib\site-packages\django\conf\__init__.py", line 151, in __init__
    raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")
django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.



  ##Setting.py file starts here



import os
from django.db.models.loading import get_app
from oscar import get_core_apps
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
from base import *
SECRET_KEY = '8m%99(yb#ulqf4@bq*s$(x9vup2kk5=0q@4aif+fhoidfnukz%'
DEBUG = True
TEMPLATE_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',
'django.contrib.flatpages',
'django.contrib.sites',
'south',
'compressor',
 ]+get_core_apps()
 SITE_ID=1

 MIDDLEWARE_CLASSES = (
'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',
'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
'oscar.apps.basket.middleware.BasketMiddleware',
 )

ROOT_URLCONF = 'suresh.urls'

WSGI_APPLICATION = 'suresh.wsgi.application'


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

# Internationalization

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

STATIC_URL = '/static/'


TEMPLATE_CONTEXT_PROCESSORS = (
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.request",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.static",
"django.core.context_processors.tz",
"django.contrib.messages.context_processors.messages",
'oscar.apps.search.context_processors.search_form',
'oscar.apps.promotions.context_processors.promotions',
'oscar.apps.checkout.context_processors.checkout',
'oscar.apps.customer.notifications.context_processors.notifications',
'oscar.core.context_processors.metadata',

)



 AUTHENTICATION_BACKENDS = (
 'oscar.apps.customer.auth_backends.Emailbackend',
'django.contrib.auth.backends.ModelBackend',
 )


from oscar import OSCAR_MAIN_TEMPLATE_DIR
TEMPLATE_DIRS = (
 # location('templates'),
  OSCAR_MAIN_TEMPLATE_DIR,
 )
 from oscar.defaults import *


 HAYSTACK_CONNECTIONS = {
   'default': {
      'ENGINE': 'haystack.backends.simple_backend.SimpleEngine',
   },
  }

答案 2 :(得分:2)

如果您在项目目录中缺少__init__.py,也会发生这种情况,因为Python不会将该目录识别为模块。