【发布时间】:2016-03-29 17:11:34
【问题描述】:
我有一个非常简单的 Django 模板(不过我是一个前端的菜鸟),我想在服务器的 IP 地址更改后进行 javascript 重定向。
这实际上是服务器配置站点的一部分,允许更改其 IP(例如在路由器管理 Web 控制台上)。
页面向用户显示服务器将重新启动的信息,同时服务器异步初始化重新启动序列。延迟一段时间后,一个简单的 javascript oneliner 会尝试在新 IP 地址上重新连接服务器。
在此示例中,我尝试将 IP 从:192.168.1.31 更改为:192.168.1.41,但在显示信息页面后:
# Server is now rebooting. If page doesn't open within 30 seconds, please refresh it with F5 button.
# Redirecting to: 192.168.1.41:8080/config
...我正在进入以下错误页面:
# Page not found (404) Request Method: GET Request URL:
# http://192.168.1.31:8080/config/192.168.1.41:8080/config
#
# Using the URLconf defined in cfgviewer.urls, Django tried these URL
# patterns, in this order:
#
# ^config/ ^$ [name='index']
# ^admin/
#
# The current URL, config/192.168.1.41:8080/config, didn't match any of
# these.
#
# You're seeing this error because you have DEBUG = True in your Django
# settings file. Change that to False, and Django will display a
# standard 404 page.
当 IP 地址更改时,主页 index.html 会重定向到 reboot.html,并在 POST 请求中将 redirect_address 传递给它。
片段形式index.html:
(...)
context = {
(...)
'redirect_address': redirect_address,
}
return render(request, 'cfgpanel/reboot.html', context)
(...)
Django reboot.html 模板代码:
{% load staticfiles %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Server Configuration - rebooting...</title>
</head>
<body>
<h1>Server is now rebooting. If page doesn't open within 30 seconds, please refresh it with F5 button.</h1>
{% if redirect_address != "" %}
<div>
<h3 class="error_msg">Redirecting to: {{ redirect_address }}:8080/config</h3>
<script type="text/javascript">
window.setTimeout(function() {
var new_url = "{{ redirect_address }}:8080/config";
window.location.replace(new_url)
}, 30000);
</script>
</div>
{% endif %}
</body>
</html>
Django urls.py 代码:
from django.conf.urls import url
from . import views
app_name = "config"
urlpatterns = [
url(r'^$', views.index, name='index'),
]
问题:为什么客户端/浏览器不尝试访问我传递给它的绝对地址?如何让它打开192.168.1.41:8080/config 而不是http://192.168.1.31:8080/config/192.168.1.41:8080/config?
【问题讨论】:
-
您是否尝试在
redirect_address前面加上“http://”? -
不可能!你知道吗?有效!谢谢!
标签: javascript python django redirect