django: You may need to add 'localhost' to ALLOWED_HOSTS

python manage.py runserver后页面访问失败,提示:
DisallowedHost at /admin/
Invalid HTTP_HOST header: ‘localhost:8000’. You may need to add ‘localhost’ to ALLOWED_HOSTS.

解决方法:
这里项目名称为test
打开test/test/settings.py配置文件,找到ALLOWED_HOSTS配置项
正常情况下,该配置项默认为空:ALLOWED_HOSTS=[],此时则ALLOWED_HOSTS默认包括了localhost
而我之前修改了这个配置:ALLOWED_HOSTS=['adminserver'],这时只能够通过adminserver访问,localhost不可以
原因是目前ALLOWED_HOST不为空,所以localhost不会默认添加进去,于是我添加了一项localhost进去,才可以正常访问,即ALLOWED_HOSTS = ['adminserver', 'localhost']
也可改为ALLOWED_HOSTS = ['*'],允许任意host访问

你可能感兴趣的:(django)