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

1,612 views
Skip to first unread message

Avitab Ayan Sarmah

unread,
May 13, 2018, 3:02:00 AM5/13/18
to Django users
Hi all,
        while going through the dajngo tutorial i stuck while executing a code i.e., mentioned below. It is shoeing "Invalid HTTP_HOST header: 'testserver'. You may need to add 'testserver' to ALLOWED_HOSTS.".


the code is written in python manage.py shell.i.e.,

PS C:\Users\AVITABAYAN\mysite> python manage.py shell
Python 3.6.2 (v3.6.2:5fd33b5, Jul  8 2017, 04:57:36) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.test import Client
>>> #...
>>> client = Client()
>>> #...
>>> response = client.get('/')
Invalid HTTP_HOST header: 'testserver'. You may need to add 'testserver' to ALLOWED_HOSTS.

please find what is error and comment.Thank you

Andréas Kühne

unread,
May 13, 2018, 6:42:09 AM5/13/18
to django...@googlegroups.com
Hi,

The error is pretty self explanatory. When the test client calls the django server it uses the hostname of testserver. This works while testing and also in development mode as long as you have DEBUG=True in your settings file.

If you have changed that to False, django will look in the list of ALLOWED_HOSTS in the settings file (which should be set to the hostnames that your server allows) - but you shouldn't require it for testing. 

If you have set DEBUG to False and set the ALLOWED_HOSTS setting to the production server and want to test something with the django test client you can initialize the client with the following statement:

client = Client(SERVER_NAME='www.example.com') 

and change to the correct server name.

Regards,

Andréas

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/53313ac7-6840-4ee9-b49b-804f91a045d4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Avitab Ayan Sarmah

unread,
May 15, 2018, 11:57:16 AM5/15/18
to Django users
hi Andreas,

Andréas

To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages