gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 10:29:08 25 4
gpt4 key购买 nike

我正在关注来自 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=offexport ALLOWED_HOSTS=localhost,example.com我确定我有环境变量:

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

谁能告诉我哪里出了问题?

最佳答案

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

关于python - 轻量级 Django : settings. ALLOWED_HOSTS 设置为环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27936656/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com