Django Invalid HTTP_HOST header: 'testserver'. You may need to add u'testserver' to ALLOWED_HOSTS
Django Invalid HTTP_HOST header: 'testserver'. You may need to add u'testserver' to ALLOWED_HOSTS
I started learning Django, I'm in the middle of implementing "Test a view" functionality.
When I use test Client in the shell, the exception has occurred as follows.
Invalid HTTP_HOST header: 'testserver'. 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
In the tutorial, 404 should be appeared, but I get 400.
When I continue running the command as follows, same exception has occurred.
>>> response = client.get(reverse('polls:index'))
>>> response.status_code
400
but the result must be 200.I guess I should declare ALLOWED_HOSTS in the settings.py, but how can I?
I run the server on localhost using $python manage.py runserver.
I wanna know the reason and solution.
Here is settings.py as follows.
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
Thanks for contacting me. Yeah, debug is false
– Yuiry Kozlenko
May 25 '17 at 15:31
then inside allowed host put 127.0.0.1,localhost and check
– Exprator
May 25 '17 at 15:32
Should I declare ALLOWED_HOSTS in the settings.py file?
– Yuiry Kozlenko
May 25 '17 at 15:33
there is an allowed host variable just after debug, inside it put it , if you dont have it, declare it with all caps and put the 2 inside like ALLOWED_HOSTS=[127.0.0.1,'localhost']
– Exprator
May 25 '17 at 15:35
4 Answers
4
ALLOWED_HOSTS = ['127.0.0.1', 'localhost']
put it like this
That's it. Thanks for your help:).
– Yuiry Kozlenko
May 25 '17 at 15:49
@YuiryKozlenko you are welcome bro
– Exprator
May 25 '17 at 15:50
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',
]
adding 'testserver' worked for me here.
– rick debbout
May 11 at 20:00
You should edit it like that:
ALLOWED_HOSTS = [
'172.0.0.1',
'localhost',
'testserver',
]
settings.py is in read-only mode
ALLOWED_HOSTS = ['127.0.0.1', 'localhost']
this is how to save it
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
is your debug false?
– Exprator
May 25 '17 at 15:31