Reputation: 665
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
Upvotes: 40
Views: 77702
Reputation: 1
Please run below command in python shell in your app folder: Django Tutorial: Writing Unit Test
from django.test.utils import setup_test_environment
setup_test_environment()
Upvotes: 0
Reputation: 31
In settings.py
file, you can simply update ALLOWED_HOSTS = ['*']
. But yes, you can also use solutions provided by others too but to keep it short and simple you can go with this
Upvotes: 0
Reputation: 2222
Adding 'testserver', 'localhost', or '127.0.0.1' did not work for me (Django >3.1).
What did was initiating the client with a different server name:
c = Client(SERVER_NAME='localhost')
Note that I got an error mentioning I needed to add 'testserver', but that I initiate the client with 'localhost'.
Upvotes: 12
Reputation: 27533
Edit the following line in your settings.py
file:
ALLOWED_HOSTS = ['127.0.0.1', 'localhost']
Restart your server afterwards
Upvotes: 53
Reputation: 500
This worked for me
you can try this:
ALLOWED_HOSTS += ['testserver']
Upvotes: 3
Reputation: 642
You should edit it like that:
ALLOWED_HOSTS = [ '127.0.0.1', 'localhost', 'testserver', ]
Upvotes: 5
Reputation: 2288
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=('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.
If you want to make it a tuple, you need to add a comma like this:
ALLOWED_HOSTS=('testserver',)
This works as expected.
Upvotes: 0
Reputation: 1
settings.py is in read-only mode
ALLOWED_HOSTS = ['127.0.0.1', 'localhost']
this is how to save it
Upvotes: 0
Reputation: 121
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',
]
Upvotes: 12