Python Django 无效的 HTTP_HOST 标头:'testserver'。您可能需要将 u'testserver' 添加到 ALLOWED_HOSTS

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44184268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:47:53  来源:igfitidea点击:

Django Invalid HTTP_HOST header: 'testserver'. You may need to add u'testserver' to ALLOWED_HOSTS

pythondjango

提问by Yuiry Kozlenko

I started learning Django, I'm in the middle of implementing "Test a view" functionality. When I use test Clientin the shell, the exception has occurred as follows.

我开始学习 Django,我正在实现“测试视图”功能。当我在shell中使用test Client时,出现了如下异常。

Invalid HTTP_HOST header: 'testserver'. You may need to add u'testserver' to ALLOWED_HOSTS.

无效的 HTTP_HOST 标头:'testserver'。您可能需要将 u'testserver' 添加到 ALLOWED_HOSTS。

I run the command in the shell as follows.

我在 shell 中运行命令如下。

>>> from django.test.utils import setup_test_environment
>>> setup_test_environment()
>>> from django.test import Client
>>> client = Client()
>>> response = client.get('/')
>>> response.status_code
400

In the tutorial, 404 should be appeared, but I get 400. When I continue running the command as follows, same exception has occurred.

在教程中,应该出现404,但我得到400。当我继续运行如下命令时,发生了同样的异常。

>>> response = client.get(reverse('polls:index'))
>>> response.status_code
400

but the result must be 200.I guess I should declare ALLOWED_HOSTSin the settings.py, but how can I? I run the server on localhost using $python manage.py runserver.

但结果必须是 200。我想我应该在settings.py 中声明ALLOWED_HOSTS,但我该怎么做?我使用$python manage.py runserver在本地主机上运行服务器。

I wanna know the reason and solution.

我想知道原因和解决方案。

Here is settings.py as follows.

这里是 settings.py 如下。

import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SECRET_KEY = '8v57o6wyupthi^#41_yfg4vsx6s($x0xmu*95_u93wwy0_&u'
DEBUG = True
ALLOWED_HOSTS = [127.0.0.1,'localhost']
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'polls',
]
....    (MIDDLEWARE)
ROOT_URLCONF = 'tutorial.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'tutorial.wsgi.application'
....    (DATABASES, AUTH_PASSWORD_VALIDATORS)
LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

回答by Exprator

ALLOWED_HOSTS = ['127.0.0.1', 'localhost']

put it like this

把它像这样

Restart your server afterwards

之后重启你的服务器

回答by khjoony

ALLOWED_HOSTS = ['XXX.iptime.org', 'localhost', '127.0.0.1', 'testserver']


# Application definition

INSTALLED_APPS = [
    'polls.apps.PollsConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

回答by oussama boumaad

You should edit it like that:

你应该像这样编辑它:

ALLOWED_HOSTS = [ '127.0.0.1', 'localhost', 'testserver', ]

ALLOWED_HOSTS = [ '127.0.0.1', 'localhost', 'testserver', ]

回答by Amit Panasara

You can try for testing purpose

您可以尝试用于测试目的

ALLOWED_HOSTS = ['*']

回答by Muhammad Shahid Rafi

settings.py is in read-only mode

settings.py 处于只读模式

ALLOWED_HOSTS = ['127.0.0.1', 'localhost']

this is how to save it

这是如何保存它

回答by M Haziq

Apart from the correct answers, there is an important check which you need to keep in mind. Setting ALLOWED_HOSTS with a single valued tuple will still give you same error, for example if you set it this way:

除了正确的答案,还有一个重要的检查你需要记住。使用单值元组设置 ALLOWED_HOSTS 仍然会给你同样的错误,例如,如果你这样设置:

ALLOWED_HOSTS=('testserver')

It does not work, because you may wanted to make this a tuple but ACTUALLY it is a string in Python, yes thats strange but true! you can read more about tuples here: tuples.

它不起作用,因为您可能想让它成为一个元组,但实际上它是 Python 中的一个字符串,是的,这很奇怪,但确实如此!您可以在此处阅读有关元组的更多信息:tuples

If you want to make it a tuple, you need to add a comma like this:

如果你想把它变成一个元组,你需要像这样添加一个逗号:

ALLOWED_HOSTS=('testserver',)

This works as expected.

这按预期工作。