简体   繁体   English

轻量级Django:settings.ALLOWED_HOSTS设置为环境变量

[英]Lightweight Django: settings.ALLOWED_HOSTS set as environment variable

I'm following Lightweight Django by Julia Elman and Mark Lavin, from O'Reilly. 我正在关注来自O'Reilly的Julia Elman和Mark Lavin的“轻量级Django”。 On Chapter 1, I can't set ALLOWED_HOSTS as an environment variable. 在第1章中,我无法将ALLOWED_HOSTS设置为环境变量。

When I run python hello.py runserver , I keep have the CommandError: You must set settings.ALLOWED_HOSTS if DEBUG is False 当我运行python hello.py runserver ,出现CommandError: You must set settings.ALLOWED_HOSTS if DEBUG is False

This is my hello.py : 这是我的hello.py

import os
import sys
from django.conf import settings

DEBUG = os.environ.get('DEBUG', 'on') == 'on'
SECRET_KEY = os.environ.get('SECRET_KEY', os.urandom(32))
ALLOWED_HOSTS = os.environ.get('ALLOWED_HOSTS', 'localhost').split(',')

settings.configure(
    DEBUG=DEBUG,
    SECRET_KEY=SECRET_KEY,
    ROOT_URLCONF=__name__,
    MIDDLEWARE_CLASSES=(
        'django.middleware.common.CommonMiddleware',
        'django.middleware.csrf.CsrfViewMiddleware',
        'django.middleware.clickjacking.XFrameOptionsMiddleware',
    ),
)

from django.conf.urls import url
from django.core.wsgi import get_wsgi_application
from django.http import HttpResponse

def index(request):
    return HttpResponse('Hello World')

urlpatterns = (
    url(r'^$', index),
)

application = get_wsgi_application()

if __name__ == "__main__":
    from django.core.management import execute_from_command_line
    execute_from_command_line(sys.argv)

I've done the export DEBUG=off , and export ALLOWED_HOSTS=localhost,example.com and I'm sure I have the environment variable: 我已经完成了export DEBUG=off ,并export ALLOWED_HOSTS=localhost,example.com并且我确定我具有环境变量:

$printenv DEBUG
off
$printenv ALLOWED_HOSTS
localhost,example.com

Can somebody tell me what's wrong? 有人可以告诉我怎么了吗?

您忘记将ALLOWED_HOSTS参数添加到settings.configure()调用中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM