django error: DisallowedHost: Invalid HTTP_HOST header: ''. You may need to add u'' to ALLOWED_HOST

测试环境:

[root@nanx-lli ~]# lsb_release -a
LSB Version: :core-4.1-amd64:core-4.1-noarch
Distributor ID: CentOS
Description: CentOS Linux release 7.4.1708 (Core)
Release: 7.4.1708
Codename: Core


[root@nanx-lli ~]# python
Python 2.7.5 (default, Aug 4 2017, 00:39:18)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> django.VERSION
(1, 11, 12, u'final', 0)

[root@nanx-lli HelloWorld]# pwd
/root/HelloWorld/HelloWorld
[root@nanx-lli HelloWorld]# tree
.
├── __init__.py
├── __init__.pyc
├── settings.py
├── settings.pyc
├── urls.py
├── urls.pyc
├── view.py
├── view.pyc
├── wsgi.py
└── wsgi.pyc

0 directories, 10 files
[root@nanx-lli HelloWorld]# cat view.py

from django.http import HttpResponse

def hello(request):
return HttpResponse("Hello world!")

问题:

PC: 10.245.36.203上通过chrome访问,http://10.245.242.221:8000时报错,并且此时在Linux上同时有错误log显示:

Invalid HTTP_HOST header: '10.245.242.221:8000'. You may need to add u'10.245.242.221' to ALLOWED_HOSTS.

解决方案:

添加10.245.242.221到allowed_hosts.

修改setting.py

......

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ['10.245.242.221']

# Application definition

......

猜你喜欢

转载自www.cnblogs.com/studyddup0212/p/8930258.html