无法从本地主机访问 Django

Django Cannot Be Accessed From 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 也

只需尝试 http://0.0.0.0:8000 而不是 localhost:8000

默认 localhost 值为 http://127.0.0.1

如果您看到 运行服务器结果,您有:

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

只需执行

python manage.py runserver 

你可以通过本地主机自动访问,如果没有锁定端口 8000,你可以访问外部访问,要访问 django admin,请使用下一个 url:

http://127.0.0.1:8000/admin/

你的主机文件怎么会变成这样?? .你能测试一下吗。

# 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 做同样的事情并试一试吗......

如果我 运行 python manage.py runserver

我无法从我的浏览器访问 localhost:8000

我不知道为什么这个命令在我的笔记本电脑上不起作用,但在我的电脑上却可以。

====

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

python manage.py runserver [::]:8000

在你项目文件夹的settings.py文件中, 将 '0.0.0.0' 添加到 允许的主机 列表中。 然后保存文件,运行 和 command.Guess 将起作用。

我也是 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。有人可以纠正我吗?