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