• 访问Django项目出现DisallowedHost at / Invalid HTTP_HOST header问题


    闲来无事,想玩玩django,源码安装碰到了一堆乱七八糟依赖性问题,耗费一下午的时间总算是在ubuntu14.04上搭建好了python3+django2开发环境,

    心血来潮,创建了一个django项目,启动运行,好吧又碰到问题了。如下:

    DisallowedHost at /
    Invalid HTTP_HOST header: '192.168.1.156:8000'. You may need to add '192.168.1.156' to ALLOWED_HOSTS.
    Request Method:    GET
    Request URL:    http://192.168.1.156:8000/
    Django Version:    2.0
    Exception Type:    DisallowedHost
    Exception Value:    
    Invalid HTTP_HOST header: '192.168.1.156:8000'. You may need to add '192.168.1.156' to ALLOWED_HOSTS.
    Exception Location:    /usr/local/lib/python3.4/dist-packages/django/http/request.py in get_host, line 105
    Python Executable:    /usr/bin/python3
    Python Version:    3.4.3
    Python Path:    
    ['/home/liuxin/work/python/django/mysite',
     '/usr/lib/python3.4',
     '/usr/lib/python3.4/plat-i386-linux-gnu',
     '/usr/lib/python3.4/lib-dynload',
     '/usr/local/lib/python3.4/dist-packages',
     '/usr/lib/python3/dist-packages']
    Server time:    Mon, 18 Dec 2017 10:33:29 +0000
    Traceback Switch to copy-and-paste view
    /usr/local/lib/python3.4/dist-packages/django/core/handlers/exception.py in inner
                response = get_response(request) ...
    ▶ Local vars
    /usr/local/lib/python3.4/dist-packages/django/utils/deprecation.py in __call__
                response = self.process_request(request) ...

    猜测了下大概可能也许八成..是权限的问题,我的ubuntu是安装在虚拟机中的,于是我试了一下在虚拟机中通过浏览器能够正常访问,但是在我的pc机上就不能访问,这下可以断定,是因为权限问题,上网百度了一下,django工程项目中settings.py的文件,这个文件是项目的一些配置设置,打开这个文件找到“ALLOWED_HOSTS”关键字的位置

    默认这个列表是空的(也就是指允许本机访问)

    ALLOWED_HOSTS = []

    修改这个列表:

    ALLOWED_HOSTS = ['*'] # 允许所有的主机

    当然也可以指定可访问主机的ip:

    ALLOWED_HOSTS = ['198.211.99.20', 'localhost', '127.0.0.1']

    ok,问题解决,下班回家。

  • 相关阅读:
    Understand each field of AWR [ID 884046.1]
    Linux iptables 防火墙 添加删除 端口
    SCSI 总线和协议
    How to create physical standby database with 11g RMAN DUPLICATE FROM ACTIVE DATABASE [ID 747250.1]
    How to create physical standby database with 11g RMAN DUPLICATE FROM ACTIVE DATABASE [ID 747250.1]
    Linux iptables 防火墙 添加删除 端口
    Oracle alert log 按天 存放 脚本
    文件系统 和 网络附接存储(NAS: Network Attached Storage)
    SCSI 总线和协议
    IP(Internet Protocal) 地址 说明
  • 原文地址:https://www.cnblogs.com/enfp/p/8059114.html
Copyright © 2020-2023  润新知