簡體   English   中英

輕量級Django:settings.ALLOWED_HOSTS設置為環境變量

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

我正在關注來自O'Reilly的Julia Elman和Mark Lavin的“輕量級Django”。 在第1章中,我無法將ALLOWED_HOSTS設置為環境變量。

當我運行python hello.py runserver ,出現CommandError: You must set settings.ALLOWED_HOSTS if DEBUG is False

這是我的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)

我已經完成了export DEBUG=off ,並export ALLOWED_HOSTS=localhost,example.com並且我確定我具有環境變量:

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

有人可以告訴我怎么了嗎?

您忘記將ALLOWED_HOSTS參數添加到settings.configure()調用中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM