在Apache中错误配置SECRET_KEY(错误日志),但在runserver

时间:2017-11-24 06:10:04

标签: django

这是完整的错误

django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty

我在GNU / Linux(Arch)中开发并部署到Windows 10.Django的内部服务器runserver适用于这两种环境,但Apache在这两种环境中都会抛出上述错误。

我在设置文件夹(包)中使用base.py,local.py和production.py分别设置了文件。 SECRET_KEY在base.py中设置,local.py和production.py都从base.py中导入*。之前我尝试过像.bashrc这样的环境变量设置

export MYPROJECT_SECRET_KEY="very_long_fake_key"

但简化回字符串版本,但问题仍然存在。

我部署到pythonany的PAAS服务,并在那里工作。

Apache httpd.conf设置(由Django docs提供):

#mod_wsgi
LoadModule wsgi_module modules/mod_wsgi.so

WSGIScriptAlias / /home/shgr/projects/autozap/autozap/wsgi.py
WSGIPythonHome /home/shgr/.virtualenvs/autozap
WSGIPythonPath /home/shgr/projects/autozap/

<Directory /home/shgr/projects/autozap/autozap>
<Files wsgi.py>
Require all granted
</Files>
</Directory>

Alias /robots.txt /home/shgr/projects/autozap/static/robots.txt
#Alias /favicon.ico /home/shgr/projects/autozap/favicon.ico

Alias /media/ /home/shgr/projects/autozap/media/
Alias /static/ /home/shgr/projects/autozap/static/

<Directory /home/shgr/projects/autozap/static>
Require all granted
</Directory>

<Directory /home/shgr/projects/autozap/media>
Require all granted
</Directory>

#import_files
Alias /import_files /home/shgr/projects/autozap/webdata/import_files
<Directory /home/shgr/projects/autozap/webdata/import_files>
Require all granted
</Directory>

base.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.dirname(__file__)))
REACT_APP_DIR = os.path.join(BASE_DIR, 'frontend')

# 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!
#SECRET_KEY = os.environ.get("AUTOZAP_SECRET_KEY")
SECRET_KEY = "$hg@z4nus7^6!s7d-ku4ahjew_@!+&u1+*s7sq1--fake-fake"

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

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'rest_framework',
    'website',
]
# 'django.middleware.csrf.CsrfViewMiddleware'
MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

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


# Password validation
# https://docs.djangoproject.com/en/1.11/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/1.11/topics/i18n/

LANGUAGE_CODE = 'ru-ru'

TIME_ZONE = 'Asia/Krasnoyarsk'

USE_I18N = True

USE_L10N = True

USE_TZ = True

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/

STATIC_URL = '/static/'

STATIC_ROOT = 'static'

REST_FRAMEWORK = {
   'UPLOADED_FILES_USE_URL': False
}

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)

1 个答案:

答案 0 :(得分:0)

好的,我弄清楚我做错了什么。首先,我在〜/ .bashrc

中导出环境变量
export DJANGO_SETTINGS_MODULE=autozap.settings.local
export AUTOZAP_SECRET_KEY="$hg@z4nus7^6!s7d-ku4ahjew_@!+&u1--fakefake"

这是错误的(它解释了为什么它只适用于runserver)https://stackoverflow.com/a/35241699/7945930

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

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

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

但它只适用于字符串版本的密钥,更重要的是没有在runserver中工作(仅限apache)。 django docs说,后者是首选方式。

但我最终将我的秘密变量存储在__init__.py中。感谢这个答案https://stackoverflow.com/a/15315143/7945930

现在我的/autozap/autozap/settings/__init__.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)环境中使用。

P.S。仍然没有得到apache最初使用环境变量和没有上述配置的方式。