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

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

Posted by: admin November 30, 2017 Leave a comment

Questions:

I use Django 1.6.5 in my program txsite with the settings:

DEBUG = True

I set DEBUG to False, but when I runserver, I get the following error:

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

I tried setting ALLOWED_HOSTS = ['127.0.0.1', 'localhost'] but I get the same error.

anyone know how to figure it out?

Answers:

Try

ALLOWED_HOSTS = ['*']

Less secure if you’re not firewalled off or on a public LAN, but it’s what I use and it works.

EDIT: Interestingly enough I’ve been needing to add this to a few of my 1.8 projects even when DEBUG = True. Very unsure why.

Questions:
Answers:

Your solution might be to add the original IP and/or hostname also

ALLOWED_HOSTS = ['localhost', '127.0.0.1', '111.222.333.444', 'mywebsite.com']

Basically the host header (or X-Forwarded-Host if USE_X_FORWARDED_HOST is enabled) should match one of the values in ALLOWED_HOSTS.

Questions:
Answers:

Make sure it’s not redefined again lower down in your settings.py. The default settings has:

ALLOWED_HOSTS = []

Questions:
Answers:

From documentation:
https://docs.djangoproject.com/en/1.10/ref/settings/

if DEBUG is False, you also need to properly set the ALLOWED_HOSTS
setting. Failing to do so will result in all requests being returned
as “Bad Request (400)”.

And from here:
https://docs.djangoproject.com/en/1.10/ref/settings/#std:setting-ALLOWED_HOSTS

I am using something like this:

ALLOWED_HOSTS = ['localhost', '127.0.0.1', 'www.mysite.com']

Leave a Reply

Your email address will not be published. Required fields are marked *