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

53👍

Edit the following line in your settings.py file:

ALLOWED_HOSTS = ['127.0.0.1', 'localhost']

Restart your server afterwards

12👍

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',
]

8👍

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

👤Raoul

5👍

You should edit it like that:

ALLOWED_HOSTS = [
‘127.0.0.1’,
‘localhost’,
‘testserver’,
]

3👍

This worked for me
you can try this:

ALLOWED_HOSTS += ['testserver']

2👍

You can try for testing purpose

ALLOWED_HOSTS = ['*']

0👍

settings.py is in read-only mode

ALLOWED_HOSTS = ['127.0.0.1', 'localhost']

this is how to save it

0👍

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.

👤Haziq

0👍

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

Leave a comment