在django中实现页面倒数几秒后自动跳转的例子

yipeiwu_com6年前Python基础

实现倒计时跳转要和html中的js结合起来,

例如:实现一个页面简单的注册,然后注册成功后倒计时自动跳转到登录页面。

# 注册页面
def register(request):

 return render(request,"register.html")
# 点击注册
def doregister(request):
 # 获得用户输入的信息,保存到数据库
 username=request.GET.get("username")
 password=request.GET.get("password")
 surepwd=request.GET.get("surepwd")
 age=request.GET.get("age")

 # 判断密码
 if surepwd==password :
 # 创建一个对象
 user = User()
 user.u_name = username
 # user.u_password=password

 # 创建md5对象
 MD5 = hashlib.md5()
 # 将一个二进制数据进行md5处理,生成一个128位的二进制数据
 MD5.update(password.encode("utf-8"))
 # 将二进制结果转换成 十六进制的结果,4位二进制转换成1位十六进制
 passwd = MD5.hexdigest()
 user.u_password = passwd
 user.u_age = int(age)
 # token是唯一的
 # 生成一个无法重复的标识
 user.u_token = createToken()
 user.save()

 return render(request,'pageJump.html')
 else:
 return render(request,'register.html',context={"alert":1})

return render(request,‘pageJump.html')用来请求实现自动跳转的页面。

<body>
<p>注册成功!还有<span id="sp">5</span>秒跳转到登录界面...</p>
<script>
 onload=function () {
 setInterval(go,1000)
 };
 var x=4;
 function go() {

 if (x>=0){
  document.getElementById("sp").innerText=x;
 }else {
  location.href="/day06/index" rel="external nofollow" rel="external nofollow" ;
 }
 x--;
 }
</script>
</body>

setInterval(go,1000)设置定时器,1秒执行一次go函数,当x小于0时,执行 location.href="/day06/index" rel="external nofollow" rel="external nofollow" ;这样就能跳转到登录页面,去执行登录操作了

以上这篇在django中实现页面倒数几秒后自动跳转的例子就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python实现可以断点续传和并发的ftp程序

前言 下载文件时,最怕中途断线,无法成功下载完整的文件。断点续传就是从文件中断的地方接下去下载,而不必重新下载。这项功能对于下载较大文件时非常有用。那么这篇文章就来给大家分享如何利用py...

Django 前后台的数据传递的方法

Django 从后台往前台传递数据时有多种方法可以实现。 最简单的后台是这样的: from django.shortcuts import render def main_page...

python线程的几种创建方式详解

Python3 线程中常用的两个模块为: _thread threading(推荐使用) 使用Thread类创建 import threading from time...

PyQt5的安装配置过程,将ui文件转为py文件后显示窗口的实例

PyQt5安装 在cmd下输入pip install PyQt5 完成PyQt5安装, 安装完成后,在python安装目录下可以看到 配置PyCharm 配置PyCharm是为了在P...

Python的shutil模块中文件的复制操作函数详解

copy() chutil.copy(source, destination) shutil.copy() 函数实现文件复制功能,将 source 文件复制到 destination 文...