如何解决SuspiciousOperation:无效的HTTP_HOST标头错误?

Ash*_*ary 2 python apache django pythonanywhere

我试图在pythonanywhere上部署我的django应用程序.

从apache错误日志回溯:

2013-07-23 09:31:35,058 :Internal Server Error: /favicon.ico
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 89, in get_response
    response = middleware_method(request)
  File "/usr/local/lib/python2.7/dist-packages/django/middleware/common.py", line 55, in process_request
    host = request.get_host()
  File "/usr/local/lib/python2.7/dist-packages/django/http/__init__.py", line 179, in get_host
    "Invalid HTTP_HOST header (you may need to set ALLOWED_HOSTS): %s" % host)
SuspiciousOperation: Invalid HTTP_HOST header (you may need to set ALLOWED_HOSTS): monty_singh.pythonanywhere.com
Run Code Online (Sandbox Code Playgroud)

我试过['*']['monty_singh.pythonanywhere.com']作为值,ALLOWED_HOSTS但应用程序仍然无法正常工作.无论我设置什么值,ALLOWED_HOSTS我都会得到同样的错误.

ayc*_*dee 6

当Django从HTTP请求验证主机时,它会尝试将它与以下正则表达相匹配,您可以在源代码中找到它:

host_validation_re = re.compile(r"^([a-z0-9.-]+|\[[a-f0-9]*:[a-f0-9:]+\])(:\d+)?$")
Run Code Online (Sandbox Code Playgroud)

用户名中的下划线导致Django拒绝请求中的主机名.您可以使用新用户名创建新的PythonAnywhere帐户,也可以使用其他Web框架.