Azure Django 应用程序有 SECRET_KEY 异常

问题描述

我使用 GitHub 在 azure 上部署了我的 django 网络应用程序,但我得到的错误是:

django.core.exceptions.ImproperlyConfigured:SECRET_KEY 设置不能为空。

我的 settings.py 文件

import os

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

SECRET_KEY = os.getenv('SECRET_KEY')

DEBUG = False

TEMPLATE_DEBUG = DEBUG

ALLOWED_HOSTS = os.getenv('ALLOWED_HOSTS')


STATICFILES_Dirs = [
    os.path.join(BASE_DIR,'static/'),"C:/Users/ande/Documents/FC_Database/FC_Database/frontend/templates/frontend/index.html"
]


# Application deFinition

INSTALLED_APPS = [
    'django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles','SLAR','import_export','rest_framework','frontend'
]

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

ROOT_URLconf = 'FC_Database.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 = 'FC_Database.wsgi.application'

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.MysqL','NAME': os.getenv('NAME'),'USER': os.getenv('USER'),'PASSWORD': os.getenv('PASSWORD'),'PORT': 3306,'HOST': os.getenv('HOST')
    }
}

# Password validation
# https://docs.djangoproject.com/en/2.1/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.1/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.1/howto/static-files/

STATIC_URL = '/static/'

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

出于安全原因,我在 Azure 的应用程序设置中添加了密钥和其他数据库设置,但仍然无法正常工作。

enter image description here

有谁知道如何解决这个问题?

解决方法

Python 允许您使用 os.environ 字典检索环境变量的内容,其中键是您试图获取内容的变量的名称。

这是可用于从 Azure WebApp 检索环境变量的部分代码

import os
...
SECRET_KEY = os.environ['SECRET_KEY']
...

https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/