Fix Python – Django gives Bad Request (400) when DEBUG = False


Asked By – codeimplementer

I am new to django-1.6. When I run the django server with DEBUG = True, it’s running perfectly. But when I change DEBUG to False in the settings file, then the server stopped and it gives the following error on the command prompt:

CommandError: You must set settings.ALLOWED_HOSTS if DEBUG is False.

After I changed ALLOWED_HOSTS to ["",], in the browser I get the error:

Bad Request (400)

Is it possible to run Django without debug mode?

Now we will see solution for issue: Django gives Bad Request (400) when DEBUG = False


The ALLOWED_HOSTS list should contain fully qualified host names, not urls. Leave out the port and the protocol. If you are using, I would add localhost to the list too:

ALLOWED_HOSTS = ['', 'localhost']

You could also use * to match any host:


Quoting the documentation:

Values in this list can be fully qualified names (e.g. ''), in which case they will be matched against the request’s Host header exactly (case-insensitive, not including port). A value beginning with a period can be used as a subdomain wildcard: '' will match,, and any other subdomain of A value of '*' will match anything; in this case you are responsible to provide your own validation of the Host header (perhaps in a middleware; if so this middleware must be listed first in MIDDLEWARE_CLASSES).

Bold emphasis mine.

The status 400 response you get is due to a SuspiciousOperation exception being raised when your host header doesn’t match any values in that list.

This question is answered By – Martijn Pieters

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0