本地 Django 网站不会在浏览器中加载 [英] Local Django website won't load in browser

查看:36
本文介绍了本地 Django 网站不会在浏览器中加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我猜有一个非常简单的解决方案,但我搜索了每个论坛和设置指南,但无法弄清楚:

I'm guessing there's a very simple solution to this, but I searched every forum and setup guide and can't figure it out:

我在我的 local 服务器上构建了一个 Django/CentOS-6.3 环境(使用 VirtualBox 和 Vagrant).当我使用 'python manage.py runserver [::]:8000' 在 vagrant 终端中启动我的服务器时,它启动时没有错误.

I built a Django/CentOS-6.3 environment on my local server (using VirtualBox and Vagrant). When I startup my server in the vagrant terminal with 'python manage.py runserver [::]:8000' it starts up with no errors.

Validating models...

0 errors found
May 31, 2013 - 13:56:15
Django version 1.5.1, using settings 'mysitename.settings'
Development server is running at http://[::]:8000/
Quit the server with CONTROL-C.

但是,当我尝试在浏览器中导航到http://127.0.0.1:8001"时(我在 Vagrantfile 中设置了从端口 8000 到端口 8001 的端口转发),浏览器只挂起 5 分钟直到超时,然后它返回消息:

However, when I try to navigate to 'http://127.0.0.1:8001' in my browser (I set up port forwarding from port 8000 to port 8001 in my Vagrantfile), the browser just hangs for 5 minutes until it times out, then it returns the message:

> The connection was reset 
> The connection to the server was reset while the page was loading. 
> ...

这与我在关闭本地服务器后从浏览器收到的消息完全相同.我的计算机显然将此识别为转发端口,因为我尝试的任何其他端口(例如 8000)都会立即返回一个错误,指出它无法与位于 127.0.0.1:8000 的服务器建立连接.

This is the exact same message I get from the browser even after I shut down my local server. My computer obviously recognizes this as a forwarded port, because any other port I try (such as 8000) instantly returns an error saying that it can't establish a connection to the server at 127.0.0.1:8000.

关于服务器文件,我过去用 Django/Ubuntu 做过很多类似的设置,从来没有遇到过任何问题,但是 Django/CentOS 一定有一些不同的东西导致了这种情况的发生(或者也许我在我的一个服务器文件中犯了一个错误).我已按照指南设置 Django &CentOS 上的 PostgreSQL 也是如此,但无济于事.我将在下面评论我创​​建/编辑的一些文件.

With regard to the server files, I have done many similar setups with Django/Ubuntu in the past and have never had any issues, but there must be something different about Django/CentOS that is causing this to happen (or maybe I made a mistake someone in one of my server files). I have followed guides for setting up Django & PostgreSQL on CentOS, too, but to no avail. I'll comment some of the files I have created/edited below.

如果有人有解决方案,甚至有关于从哪里开始查找错误的建议,我将不胜感激.

If anyone has a solution, or even has advice on where to start looking for errors, I would very much appreciate it.

推荐答案

如果您的网络配置正确并且您的 django 应用程序与

If your network is configured correctly and your django application with

python manage.py runserver 0.0.0.0:8000

python manage.py runserver 0.0.0.0:8000

您仍然无法从 VM 主机访问您的 django 应用程序,几乎可以肯定是防火墙问题.如果您正在运行 iptables,则上述解决方案很好.

and you still can't access your django app from the VM host there is almost certainly a firewall issue. The solution above is good if you are running iptables.

我在一台 Windows 7 主机的虚拟机虚拟机上部署了 CentOS 7.我不知道这个发行版使用firewalld,而不是iptables来控制访问.

I deployed CentOS 7 on a virtualbox VM from a Windows 7 host. I didn't know that this distribution uses firewalld, not iptables to control access.

如果

ps -ae |grep 防火墙

ps -ae | grep firewall

返回类似的东西

602 ?00:00:00 防火墙d

602 ? 00:00:00 firewalld

您的系统运行的是 firewalld,而不是 iptables.它们不会一起运行.

your system is running firewalld, not iptables. They do not run together.

要更正您的 VM,以便您可以从主机访问您的 django 站点,请使用以下命令:

To correct you VM so you can access your django site from the host use the commands:

firewall-cmd --zone=public --add-port=8000/tcp --permanent

firewall-cmd --zone=public --add-port=8000/tcp --permanent

firewall-cmd --reload

firewall-cmd --reload

非常感谢 pablo v 在Access django server on virtual Machine"一文中指出这一点.

Many thanks to pablo v for pointing this out in the post "Access django server on virtual Machine".

这篇关于本地 Django 网站不会在浏览器中加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆