我有一个非常简单的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.
主页index.html在IP地址更改并在POST请求中向其传递redirect_address
时重定向到reboot.html。
代码段表单index.html:
(...)
context = {
(...)
'redirect_address': redirect_address,
}
return render(request, 'cfgpanel/reboot.html', context)
(...)
Djangoreboot.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>
Djangourls.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?
正如Kendas所建议的:
使用"http://"预处理重定向地址
完成了任务。