SECRET_KEY设置不能为空-即使在settings.py中设置了SECRET_KEY

时间:2019-10-09 17:54:32

标签: python django

当我尝试将我的models.py文件导入views.py或我的App目录中的其他任何python脚本时,我一直收到此错误,

我在settings.py中有SECRET_KEY,并且还在INSTALLED_APPS列表中添加了应用,

wsgi.py包含

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'projectname.settings')  # I replaced my project name 

manage.py还包含

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

我已经检查了StackOverflow帖子中其他人的安装。我的代码有效,但是导入模型会破坏它

我使用此命令导入我的课程

from app_name.models import subscribers

并因此定义了订阅者

  class subscribers(models.Model):
    name = models.CharField('Name', max_length=120)
    email = models.CharField('Email', max_length=120)
    access_token = models.TextField()
    expires_in = models.DateTimeField('token expiry date')

我已经执行了makemigrations和migration命令,但是作为一个新手,我不知道该去哪里。请保存一个已经3天的灵魂

谢谢。


下面是我的设置。py

"""
Django settings for ps project.

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

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

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

import os
from oauth_outlook.views import gettoken

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
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/2.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY='l!u#h@an5lj%b))=fzhv6r-3ay1&29=ls0*o1_^dxi(01x$et@'
#SECRET_KEY = os.environ.get("SECRET_KEY", "l!u#h@an5lj%b))=fzhv6r-3ay1&29=ls0*o1_^dxi(01x$et@")
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False

ALLOWED_HOSTS = ['*']


# Application definition

INSTALLED_APPS = [
    'oauth_outlook',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    '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',
    'lockdown.middleware.LockdownMiddleware',

]

ROOT_URLCONF = 'ps.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 = 'ps.wsgi.application'


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

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


# Password validation
# https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators

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',
    },
]


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

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static/")

MEDIA_URL = 'media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
LOCKDOWN_PASSWORDS = ('letmein', 'beta')

LOCKDOWN_VIEW_EXCEPTIONS = [
    gettoken,
]

1 个答案:

答案 0 :(得分:0)

看起来是以下导入导致了该问题:

from oauth_outlook.views import gettoken

在设置中导入视图或模型可能导致循环导入,即使设置了SECRET_KEY错误消息也是如此。我不喜欢django-lockout鼓励您为LOCKDOWN_VIEW_EXCEPTIONS设置导入视图的方式。

我建议您改为设置LOCKDOWN_URL_EXCEPTIONS,这样就不需要导入。

LOCKDOWN_URL_EXCEPTIONS = (
    r'^gettoken$',   # replace with gettoken view
)