Django框架用户注销功能实现方法分析


Posted in Python onMay 28, 2019

本文实例讲述了Django框架用户注销功能实现方法。分享给大家供大家参考,具体如下:

HttpResponse()里有个delete_cookie()方法专门用来删除cookie

我们到此来完整的实现一下:访问首页如果没有登录,就跳转到登录页面,登录成功之后再跳转回来的过程。

3个方法,index、login、logout

# coding:utf-8
from django.shortcuts import render,render_to_response
# Create your views here.
from django.http import HttpResponse
from UserClass import UserLogin
def index(request):
  msg = {'username':'guest'}
  if request.COOKIES.get('userlogin_username') != None :
    msg['username'] = request.COOKIES.get('userlogin_username')
  myReponse = render_to_response("index.html",msg)
  return myReponse
def login(request):
  msg = {'result': ''}
  if request.method == 'POST':
    getUserName = request.POST.get('username')
    getPwd = request.POST.get('pwd')
    # 实例化UserLogin类
    loginObj = UserLogin(getUserName,getPwd)
    if loginObj.isLogin():
      myReponse = HttpResponse("<script>self.location='/index'</script>")
      myReponse.set_cookie('userlogin_username',getUserName,3600)
      return myReponse
    else:
      msg['result'] = '用户名或密码错误'
  myReponse = render_to_response("login.html", msg)
  return myReponse
# 用户注销
def logout(request):
  r = HttpResponse()
  r.delete_cookie('userlogin_username')
  r.write("<script>self.location='/index'</script>")
  return r

首页模板index.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>首页</title>
</head>
<body>
  <h2>这是首页,当前登录用户是:{{ username }}</h2>
  {% ifequal username "guest" %}
  <p><a href="/login" rel="external nofollow" >登录</a></p>
  {% else %}
  <p><a href="/logout" rel="external nofollow" >安装退出</a></p>
  {% endifequal %}
</body>
</html>

其中用到了Django的模板语法

希望本文所述对大家基于Django框架的Python程序设计有所帮助。

Python 相关文章推荐
分享一个常用的Python模拟登陆类
Mar 29 Python
Python2.6版本中实现字典推导 PEP 274(Dict Comprehensions)
Apr 28 Python
快速入手Python字符编码
Aug 03 Python
解决matplotlib库show()方法不显示图片的问题
May 24 Python
Python实现调用另一个路径下py文件中的函数方法总结
Jun 07 Python
利用Anaconda简单安装scrapy框架的方法
Jun 13 Python
python实现AES加密和解密
Mar 27 Python
Python中的类与类型示例详解
Jul 10 Python
python递归调用中的坑:打印有值, 返回却None
Mar 16 Python
python3.4中清屏的处理方法
Jul 06 Python
Python解析微信dat文件的方法
Nov 30 Python
浅析Python模块之间的相互引用问题
Feb 26 Python
Django框架首页和登录页分离操作示例
May 28 #Python
Django框架封装外部函数示例
May 28 #Python
详解Numpy数组转置的三种方法T、transpose、swapaxes
May 27 #Python
python利用多种方式来统计词频(单词个数)
May 27 #Python
numpy中的ndarray方法和属性详解
May 27 #Python
numpy.linspace函数具体使用详解
May 27 #Python
Django利用cookie保存用户登录信息的简单实现方法
May 27 #Python
You might like
php实现在服务器端调整图片大小的方法
2015/06/16 PHP
PHP+shell实现多线程的方法
2015/07/01 PHP
PHP Imagick完美实现图片裁切、生成缩略图、添加水印
2016/02/22 PHP
CI框架简单邮件发送类实例
2016/05/18 PHP
javascript 建设银行登陆键盘
2008/06/10 Javascript
jquery中交替点击事件toggle方法的使用示例
2013/12/08 Javascript
Jquery效果大全之制作电脑健康体检得分特效附源码下载
2015/11/02 Javascript
JavaScript中windows.open()、windows.close()方法详解
2016/07/28 Javascript
基于JQuery的购物车添加删除以及结算功能示例
2017/03/08 Javascript
完美解决mui框架off-canvas侧滑超出部分隐藏无法滚动的问题
2018/01/25 Javascript
在微信小程序里使用watch和computed的方法
2018/08/02 Javascript
vue-router 实现导航守卫(路由卫士)的实例代码
2018/09/02 Javascript
原生js实现随机点餐效果
2019/12/10 Javascript
vue+Element-ui实现分页效果
2020/11/15 Javascript
[01:23]一分钟告诉你 DOTA2为什么叫信仰2
2014/06/20 DOTA
[01:03:59]2018DOTA2亚洲邀请赛3月30日 小组赛B组VGJ.T VS Secret
2018/03/31 DOTA
python合并文本文件示例
2014/02/07 Python
深入讲解Python编程中的字符串
2015/10/14 Python
分享Python开发中要注意的十个小贴士
2016/08/30 Python
python+opencv实现动态物体追踪
2018/01/09 Python
python3.5基于TCP实现文件传输
2020/03/20 Python
Python实现登陆文件验证方法
2018/10/06 Python
Python读取Pickle文件信息并计算与当前时间间隔的方法分析
2019/01/30 Python
Python实战之制作天气查询软件
2019/05/14 Python
python读写文件write和flush的实现方式
2020/02/21 Python
在ipython notebook中使用argparse方式
2020/04/20 Python
python画图时设置分辨率和画布大小的实现(plt.figure())
2021/01/08 Python
西班牙床垫网上商店:Colchones.es
2018/05/06 全球购物
销售提升方案
2014/06/07 职场文书
学校安全责任书范本
2014/07/23 职场文书
食堂厨师岗位职责
2014/08/25 职场文书
党员群众路线自我剖析材料
2014/10/06 职场文书
西岭雪山导游词
2015/02/06 职场文书
2015年效能监察工作总结
2015/04/23 职场文书
保护校园环境倡议书
2015/04/28 职场文书
导游词之广东佛山(南风古灶)
2019/09/24 职场文书