通过localhost访问django应用程序时无法通过ip地址访问django应用程序

问题描述 投票:0回答:2

我的本地计算机上有一个 django 应用程序。我可以使用以下网址从浏览器访问该应用程序:http://localhost:8000/myapp/

但是我无法使用主机的IP访问该应用程序:http://193.140.209.49:8000/myapp/我收到404错误。

我该怎么办?有什么建议吗?

python django networking
2个回答
71
投票

我假设您正在使用开发服务器。如果是这样,那么您需要专门绑定到您的外部 IP,以便服务器在那里可用。试试这个命令:

./manage.py runserver 193.140.209.49:8000

0
投票

我也有类似的问题。就我而言,我必须执行以下操作:

  • 在本地 LAN 中运行应用程序 *
python manage.py runserver 0.0.0.0:8000
http://localhost:8000
http://127.0.0.1:8000
http://192.168.0.99:8000
  • 使用公共IP运行应用程序*

端口 8080 对我来说不起作用,所以我必须添加端口 8081。

在 Windows Defender 中添加端口 8081

在互联网路由器的虚拟服务器中添加端口 8081

python manage.py runserver 0.0.0.0:8081
http://103.122.xxx.xx:8081

设置.py

ALLOWED_HOSTS = ['127.0.0.1', 'localhost', '103.122.xxx.xx', '192.168.0.99']
© www.soinside.com 2019 - 2024. All rights reserved.