![](/img/trans.png)
[英]You must set settings.ALLOWED_HOSTS if DEBUG is False. Django-r-f
[英]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.