Django Settings.py-SECRET_KEY-KeyError

时间:2019-12-10 16:00:09

标签: python django gunicorn

当前有一个 Django 项目在Python 3.6.9上运行,并使用 Gunicorn Nginx Digital ocean 上托管>。

我试图将我的SECRET_KEY和其他密码切换为环境变量,而不是将它们作为字符串存储在settings.py中。

这样做时,我遇到以下错误,Gunicorn会关闭。

Dec 10 15:22:20:   File "/home/user/projectdir/project/project/settings.py", line 23, in <module>
Dec 10 15:22:20 droplet_name gunicorn:     SECRET_KEY = os.environ['SECRET_KEY']
Dec 10 15:22:20 droplet_name gunicorn:   File "/home/user/projectdir/myenv/lib/python3.6/os.py", line 669, in __getitem__
Dec 10 15:22:20 droplet_name gunicorn:     raise KeyError(key) from None
Dec 10 15:22:20 droplet_name gunicorn: KeyError: 'SECRET_KEY'
Dec 10 15:22:20 droplet_name gunicorn: [2019-12-10 15:22:20 +0000] [20022] [INFO] Worker exiting (pid: 20022)

我已通过执行

正确地导出了SECRET_KEY

export SECRET_KEY='my_key'

这也是我的settings.py的样子:

import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

SECRET_KEY = os.environ['SECRET_KEY']

DEBUG = False

ALLOWED_HOSTS = ["my_ip"]

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

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

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'projectnamedb',
        'USER': 'myuser',
        'PASSWORD': 'mypass',
        'HOST': 'localhost',
        'PORT': '',
    }
}

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


LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

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

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

LOGIN_URL = '/home/user_login'

我已经阅读了至少十二篇类似的文章,但仍然无法解决此问题。如果可能的话,我想使用os.environ而不加载新的

更新:我已使用以下代码将test_one.py文档放置在settings.py所在的目录中。运行时,它输出SECRET_KEY并返回一个字符串作为预期的类型。在settings.py文件上仍然无法达到相同的结果。

import os

print(os.environ['SECRET_KEY'])
print(type(os.environ['SECRET_KEY']))

SECRET_KEY = os.environ['SECRET_KEY']
print(type(SECRET_KEY))

0 个答案:

没有答案