Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我不知道';“我不明白为什么会产生以下错误”;密钥设置不能为空;在执行python manage.py runserver时_Python_Django - Fatal编程技术网

我不知道';“我不明白为什么会产生以下错误”;密钥设置不能为空;在执行python manage.py runserver时

我不知道';“我不明白为什么会产生以下错误”;密钥设置不能为空;在执行python manage.py runserver时,python,django,Python,Django,我已经将SECRET\u KEY设置为windows上的环境变量,但错误表明运行开发服务器时SECRET\u KEY设置不能为空 我不明白 设置.py import os from . import storage_backends from decouple import config, Csv # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(

我已经将
SECRET\u KEY
设置为windows上的环境变量,但错误表明运行开发服务器时
SECRET\u KEY设置不能为空

我不明白

设置.py

import os
from . import storage_backends
from decouple import config, Csv

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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.environ.get('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',
    'WebBoard',
    'Search',
    'widget_tweaks',
    'storages'
]


MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    '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 = 'Board.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates'),],
        '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 = 'Board.wsgi.application'


# Database
# https://docs.djangoproject.com/en/2.0/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.0/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.0/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True



STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"),
]

# STATIC_URL = '/static/'
# MEDIA_URL = '/media/'

LOGIN_REDIRECT_URL = '/home/questions/'
LOGOUT_REDIRECT_URL = '/accounts/login/'

STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")

MEDIA_ROOT = os.path.join(BASE_DIR, "media")
MEDIA_URL = '/media/'

AWS_ACCESS_KEY_ID = config('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = config('AWS_SECRET_ACCESS_KEY')
AWS_STORAGE_BUCKET_NAME = config('AWS_STORAGE_BUCKET_NAME')


AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME


AWS_S3_OBJECT_PARAMETERS = {
    'CacheControl': 'max-age=86400',
}

AWS_S3_SIGNATURE_VERSION=True

AWS_LOCATION = 'static'

STATIC_URL = "https://%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, AWS_LOCATION)

STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
DEFAULT_FILE_STORAGE = 'Board.storage_backends.MediaStorage'

ADMIN_MEDIA_PREFIX = STATIC_URL + 'admin/'
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)


AWS_DEFAULT_ACL = None
管理.py

import os
import sys

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Board.settings")
    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH environment variable? Did you "
            "forget to activate a virtual environment?"
        ) from exc
    execute_from_command_line(sys.argv)
项目结构-

我已经将SECRET_KEY设置为环境变量-

当我直接使用
SECRET\u密钥时,而不是从环境变量中获取密钥时,它会起作用,但在使用环境变量时不会起作用


我不明白这里的问题。请帮我理解。另外,在问了这个问题后,我更改了我的
密钥。

是否将环境变量添加到开发环境中?是否将其设置为用户变量或系统变量?您是否以其他用户的身份运行开发服务器?终端中的
os.environ.get('SECRET\u KEY')
的输出是什么?尝试在pythonshell中运行它,并检查它是否在那里工作。如果它不起作用,则表示您的环境变量设置不正确。在terminal
os.environ.get(“SECRET\u KEY”)
工作正常@xxbinxxI将其设置为用户变量,并且开发服务器正在使用同一用户运行@您是否将环境变量添加到开发环境中?您是将其设置为用户变量还是系统变量?您是否以其他用户的身份运行开发服务器?终端中的
os.environ.get('SECRET\u KEY')
的输出是什么?尝试在pythonshell中运行它,并检查它是否在那里工作。如果它不起作用,则表示您的环境变量设置不正确。在terminal
os.environ.get(“SECRET\u KEY”)
工作正常@xxbinxxI将其设置为用户变量,并且开发服务器正在使用同一用户运行@约翰戈登