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

I started to learn Django, I am in the middle of the implementation of the function "Test presentation". When I use test Client in the shell, the exception occurred as follows.

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

I run the command in the shell as follows.

>>> 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 

The textbook should appear 404, but I get 400. When I continue to execute the command as follows, the same exception occurred.

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

but the result should be 200. I think I should declare ALLOWED_HOSTS in settings.py , but how can I? I am running the server on localhost using $ python manage.py runningerver.

I want to know the reason and solution.

Below is the settings.py parameter.

 import os BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) SECRET_KEY = '8v57o6wyupthi^#41_yfg4vsx6s($1$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 
+11
source share
5 answers
 ALLOWED_HOSTS = ['127.0.0.1', 'localhost'] 

say it like that

Reboot your server after

+18
source
 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', ] 
+6
source

You should edit it like this:

ALLOWED_HOSTS = ['172.0.0.1', 'Local', 'TestServer',]

+1
source

settings.py is in read-only mode

 ALLOWED_HOSTS = ['127.0.0.1', 'localhost'] 

it's how to save

0
source

Besides the correct answers, there is an important check that you need to remember. Setting ALLOWED_HOSTS with one meaningful tuple will still give you the same error, for example, if you set it like this:

 ALLOWED_HOSTS=('testserver') 

This does not work because you may have wanted to make it a tuple, but it is actually a string in Python, yes, it is strange, but true! You can read more about tuples here: tuples .

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

 ALLOWED_HOSTS=('testserver',) 

This works as expected.

0
source

Source: https://habr.com/ru/post/1268237/


All Articles