【问题标题】:Django Cannot Be Accessed From Localhost无法从 localhost 访问 Django
【发布时间】:2016-04-10 22:13:49
【问题描述】:

这有点奇怪,我无法从 localhost 访问 django,但我可以从本地 IP 访问它。

python manage.py runserver 0.0.0.0:8000

然后当我尝试从

访问时

我的主机文件

127.0.0.1 lmlicenses.wip4.adobe.com
127.0.0.1 lm.licenses.adobe.com
127.0.0.1 gc.kis.scr.kaspersky-labs.com ff.kis.scr.kaspersky-labs.com ie.kis.scr.kaspersky-labs.com
0.0.0.0 gc.kis.scr.kaspersky-labs.com ff.kis.scr.kaspersky-labs.com ie.kis.scr.kaspersky-labs.com

我猜测 Windows 防火墙或卡巴斯基有问题,但我不知道该怎么办。

我也为端口 8000 和 python.exe 添加了异常

【问题讨论】:

    标签: python django windows-7 localhost


    【解决方案1】:

    我也是 django 的初学者,但我需要以下内容才能运行服务器:

    1. settings.py 中,将ALLOWED_HOSTS 添加为
      ALLOWED_HOSTS = [ 'elearning.com' ]
      其中 elearning.com 是我的主机名。您可以使用逗号分隔多个主机。
    2. 使用以下命令运行服务器:
      python manage.py runserver [::]:8000

      python manage.py runserver 0.0.0.0:8000

      python manage.py runserver 0:8000
      它现在将监听所有接口。阅读 Django 文档here
    3. 浏览到给定的主机名。就我而言,它是http://elearning.com:8000/

    我尝试在ALLOWED_HOSTS 中写入IP,但没有成功,我无法在浏览器中打开http://192.168.x.x:8000。有人可以纠正我吗?

    【讨论】:

      【解决方案2】:

      在项目文件夹的 settings.py 文件中, 将 '0.0.0.0' 添加到 允许的主机 列表中。 然后保存文件并运行命令。猜猜就可以了。

      【讨论】:

        【解决方案3】:

        如果我运行python manage.py runserver,我将无法从浏览器访问 localhost:8000

        我不知道为什么该命令无法在我的笔记本电脑上运行,但在我的电脑上却可以运行。

        ====

        好的,所以我现在的解决方案是使用 ipv6 和端口 8000,即

        python manage.py runserver [::]:8000
        

        【讨论】:

          【解决方案4】:

          您的主机文件如何以这样的方式结束? .你能测试一下吗?

          # Copyright (c) 1993-2009 Microsoft Corp.
          #
          # This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
          #
          # This file contains the mappings of IP addresses to host names. Each
          # entry should be kept on an individual line. The IP address should
          # be placed in the first column followed by the corresponding host name.
          # The IP address and the host name should be separated by at least one
          # space.
          #
          # Additionally, comments (such as these) may be inserted on individual
          # lines or following the machine name denoted by a '#' symbol.
          #
          # For example:
          #
          #      102.54.94.97     rhino.acme.com          # source server
          #       38.25.63.10     x.acme.com              # x client host
          
          # localhost name resolution is handled within DNS itself.
          #   127.0.0.1       localhost
          #   ::1             localhost
          

          我的主机文件看到所有内容都被注释掉了。你能不能对你的hosts file 做同样的事情并试一试......

          【讨论】:

          • 好像和hosts文件没有关系,我之前试过
          • 在我的情况下确实如此。
          【解决方案5】:

          直接执行

          python manage.py runserver 
          

          自动你可以通过 localhost 访问,如果不是,你可以访问外部访问,如果它被锁定端口 8000,访问 django admin 使用下一个 url:

          http://127.0.0.1:8000/admin/
          

          【讨论】:

          • 即使只有“python manage.py runserver”,我也无法通过浏览器中的 localhost:8000 访问。这就是为什么我使用 0.0.0.0:8000 能够从我的局域网 IP 访问
          【解决方案6】:

          试试http://0.0.0.0:8000 而不是localhost:8000

          默认localhost 值为http://127.0.0.1

          如果您看到 runerver 结果:

          starting developement server at http://0.0.0.0:8000
          

          因为您在运行此命令时告诉 django 服务器从 http://0.0.0.0:8000 开始:

          python manage.py runserver 0.0.0.0:8000
          

          【讨论】:

          • 是一样的。即使使用127.0.0.1:80000.0.0.0:8000,我仍然无法访问
          • 为什么要更改 django 服务器的 IP 地址?只需运行普通命令python manage.py runserver @RichardOctovianus
          • Use runserver 0.0.0.0:8000 to make it listen on all interfaces 来自 Django 官方文档code.djangoproject.com/ticket/396
          猜你喜欢
          • 2021-07-05
          • 2017-11-06
          • 2021-07-01
          • 2020-06-25
          • 2012-10-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多