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

匿名 (未验证) 提交于 2019-12-03 02:44:02

问题:

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?

回答1:

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.



回答2:

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.



回答3:

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

ALLOWED_HOSTS = []



回答4:

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


回答5:

Just do this:

  ALLOWED_HOSTS =  ['localhost', '127.0.0.1'] 

It will do the trick.



回答6:

Just simply comment out the line: ALLOWED_HOSTS = [...]



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!