[Django]-Debugging Apache/Django/WSGI Bad Request (400) Error

109👍

Add the ALLOWED_HOSTS setting to your settings.py like so…

ALLOWED_HOSTS = [
    '.example.com', # Allow domain and subdomains
    '.example.com.', # Also allow FQDN and subdomains
]

I had this same problem and found the answer here in the docs

update: django 1.6 docs are no longer online, I updated the link to go to the django 1.7 docs for ALLOWED_HOSTS setting.

9👍

If you’ve definitely set ALOWED_HOSTS – make sure your hostname doesn’t contain underscores. It’s technically illegal.

I had to print out various functions and it boiled down to this regex failing to detect a domain in django.http

host_validation_re = re.compile(r"^([a-z0-9.-]+|\[[a-f0-9]*:[a-f0-9:]+\])(:\d+)?$")

And indeed, my domain had an underscore in it.

1👍

This is not a solution, but for debugging purposes you might set the ALLOWED_HOSTS setting in your settings.py like this

ALLOWED_HOSTS = ['*']

It should definitely work. If not, at least you will know the problem isn’t Django denying access to the given url.

Leave a comment