好久没写博客了,之前一直在忙公司的项目。
因为最近在用Python,所以顺便学了学Django框架。如果不是前后端分离的话,重定向又太麻烦,这时候提示页面也会有很大的作用。所以我就模仿TP框架自己写了个提示页面
TP框架中的error和success模板还是很好用的,主要就是给用户一个提示,接下来看一下TP中的效果
然后看一下我用python实现的效果(样式改了一下):
python中的模板方法
# success:是否为显示成功模板
# message:给用户的通知信息
# page: 要跳转的页面 默认为index
# relay:跳转延时,默认为5s
def result(request, success=True, message='暂无提示信息', page='index', relay=5):
# 如果页面为空或者未result页面,则默认返回上一页
if page == '' or page == 'result':
menu = "<script>document.getElementById('href').href=document.referrer</script>"
else:
menu = ''
# 返回动态数据
context = {}
context['message'] = message
context['page'] = page
context['success'] = success
context['relay'] = relay
context['menu'] = menu
return render(request, 'result.html', context)
调用模板
def login(request):
res = result(request, False, '用户名或密码不得为空', 'index', 3)
return res
接下来就是Html页面了,我顺便借用了boostrap
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1.0, user-scalable=no">
<link rel="stylesheet" type="text/css" href="../static/bs/bootstrap.min.css">
<script type="text/javascript" src="../static/bs/jquery-3.2.1.min.js"></script>
<script type="text/javascript" src="../static/bs/bootstrap.min.js"></script>
<title>温馨提示</title>
<style type="text/css">
* {
padding: 0;
margin: 0;
}
body {
background: #fff;
font-family: '微软雅黑';
color: #333;
font-size: 16px;
}
.system-message {
padding: 24px 48px;
}
.system-message h1 {
font-size: 100px;
font-weight: normal;
line-height: 120px;
margin-bottom: 12px;
}
.system-message .jump {
padding-top: 10px }
.system-message .jump a {
color: #333;
}
.system-message .success, .system-message .error {
line-height: 1.8em;
font-size: 28px;
color: #ee0000;
}
.system-message .detail {
font-size: 12px;
line-height: 20px;
margin-top: 12px;
display: none }
</style>
</head>
<body>
{% include "header.html" %}
<div class="system-message">
<div class="panel panel-default">
<div class="panel-body">
{% if success %}
<h3>温馨提示
<small>操作成功</small>
</h3>
<p class="success">
{{ message }}
</p>
{% else %}
<h3>温馨提示
<small>操作失败</small>
</h3>
<p class="error">
{{ message }}
</p>
{% endif %}
</div>
<div class="panel-footer">
<p class="detail"></p>
<p class="jump">
页面自动 <a id="href" href="{{ page }}">跳转</a> 等待时间: <b id="wait">{{ relay }}</b>
</p></div>
</div>
{% autoescape off %}
{{ menu }}
{% endautoescape %}
</div>
<script type="text/javascript">
(function () {
var wait = document.getElementById('wait'), href = document.getElementById('href').href;
var interval = setInterval(function () {
var time = --wait.innerHTML;
if (!(time > 0)) {
location.href = href;
clearInterval(interval);
}
}, 1000);
})();
</script>
</body>
</html>
觉得可以的话,就给个赞赏吧!