简体   繁体   中英

Lightweight Django: settings.ALLOWED_HOSTS set as environment variable

I'm following Lightweight Django by Julia Elman and Mark Lavin, from O'Reilly. On Chapter 1, I can't set ALLOWED_HOSTS as an environment variable.

When I run python hello.py runserver , I keep have the CommandError: You must set settings.ALLOWED_HOSTS if DEBUG is False

This is my 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:

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

Can somebody tell me what's wrong?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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