Django 密钥在Apache中配置不正确(错误日志),但在runserver中有效

Django 密钥在Apache中配置不正确(错误日志),但在runserver中有效,django,Django,这是全部错误 django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty 我在GNU/Linux(Arch)中开发并部署到Windows10。Django的内部服务器runserver在这两种环境中都能工作,但Apache在这两种环境中都会抛出上述错误 我在设置文件夹(包)中有单独的设置文件,其中包含base.py、local.py和production.py。SECRET_密钥在b

这是全部错误

django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty
我在GNU/Linux(Arch)中开发并部署到Windows10。Django的内部服务器
runserver
在这两种环境中都能工作,但Apache在这两种环境中都会抛出上述错误

我在设置文件夹(包)中有单独的设置文件,其中包含base.py、local.py和production.py。SECRET_密钥在base.py以及local.py和production.py import*from base.py中设置。之前,我尝试在.bashrc中设置一个环境变量,如下所示

export MYPROJECT_SECRET_KEY="very_long_fake_key"
但简化了返回字符串版本,但问题仍然存在

我将PAAS服务部署到Pythonywhere,它在那里工作

Apache httpd.conf设置(如Django文档所述):

production.py

from autozap.settings.base import *

import os

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

# SECURITY WARNING: keep the secret key used in production secret!

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

ALLOWED_HOSTS = [
    'fake.com',
    'localhost',
]

#send_mail
EMAIL_HOST = 'smtp.yandex.ru'
EMAIL_PORT = 465
EMAIL_HOST_USER = "fake@yandex.ru"
EMAIL_USE_SSL = True
EMAIL_HOST_PASSWORD = "fakefake"


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


DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'autozap',
        'USER': 'root',
        'PASSWORD': 'fake-fake',
        'HOST': 'localhost',
        'PORT': '3307',
        'sql_mode': 'STRICT_TRANS_TABLES',
    }
}
local.py

from autozap.settings.base import *

import os

DEBUG=True

ALLOWED_HOSTS = [
    'localhost'
]

INSTALLED_APPS += [
    'django.contrib.staticfiles'
]


DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'autozap',
        'USER': 'fake',
        'PASSWORD': 'fakfake',
        'HOST': 'localhost',
        'PORT': '',
        'sql_mode': 'STRICT_TRANS_TABLES',
    }
}

#send_mail
EMAIL_HOST = 'smtp.yandex.ru'
EMAIL_PORT = 465
EMAIL_HOST_USER = "fake@yandex.ru"
EMAIL_USE_SSL = True
EMAIL_HOST_PASSWORD = "fakfake"

STATICFILES_DIRS = [
    os.path.join(REACT_APP_DIR, 'build', 'static')
]
autozap/autozap/wsgi.py

import os

from django.core.wsgi import get_wsgi_application

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

application = get_wsgi_application()
manage.py

#!/usr/bin/env python
import os
import sys

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "autozap.settings")
    try:
        from django.core.management import execute_from_command_line
    except ImportError:
        # The above import may fail for some other reason. Ensure that the
        # issue is really that Django is missing to avoid masking other
        # exceptions on Python 2.
        try:
            import django
        except ImportError:
            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?"
            )
        raise
    execute_from_command_line(sys.argv)

好吧,我知道我做错了什么。首先,我在~/.bashrc中导出环境变量

export DJANGO_SETTINGS_MODULE=autozap.settings.local
export AUTOZAP_SECRET_KEY="$hg@z4nus7^6!s7d-ku4ahjew_@!+&u1--fakefake"
这是错误的(它解释了为什么它只与runserver一起工作)

我尝试将wsgi.py环境设置更改为:

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "autozap.settings.local")

但它只适用于字符串版本的密钥,更重要的是,它不适用于runserver(仅适用于apache)。django博士说,后者是首选的方式

但我最终将我的秘密变量存储在
\uuuu init\uuuuuy.py
中。多亏了这个答案

现在,我的
/autozap/autozap/settings/\uuuu init\uuuuu.py
脚本如下所示:

from autozap.settings.local import *  # or production

##### DJANGO SECRETS
SECRET_KEY = "$hg@z4nus7^6!s7d-ku4ahjew_@!+&ufakefakefake"
DATABASES['default']['PASSWORD'] = 'soo_fake'
EMAIL_HOST_USER = "notrealatall@yandex.ru"
EMAIL_HOST_PASSWORD = "very-true-password"
wsgi.py

import os

from django.core.wsgi import get_wsgi_application

os.environ["DJANGO_SETTINGS_MODULE"] = "autozap.settings"

application = get_wsgi_application()
现在,该网站可以在本地(linux)和生产(windows)环境下运行


另一方面,我仍然不明白apache最初是如何使用环境变量的,没有上述配置的。

apache之前工作了一段时间,但后来停止了。我想我做了一些不知道的事情。它最后一次起作用是在我声明玩访问控制列表之前。
os.environ["DJANGO_SETTINGS_MODULE"] = "autozap.settings.local" 
from autozap.settings.local import *  # or production

##### DJANGO SECRETS
SECRET_KEY = "$hg@z4nus7^6!s7d-ku4ahjew_@!+&ufakefakefake"
DATABASES['default']['PASSWORD'] = 'soo_fake'
EMAIL_HOST_USER = "notrealatall@yandex.ru"
EMAIL_HOST_PASSWORD = "very-true-password"
import os

from django.core.wsgi import get_wsgi_application

os.environ["DJANGO_SETTINGS_MODULE"] = "autozap.settings"

application = get_wsgi_application()