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中ConfigParse模块的用法
Sep 29 Python
浅谈python中对于json写入txt文件的编码问题
Jun 07 Python
python2 与 pyhton3的输入语句写法小结
Sep 10 Python
python 处理string到hex脚本的方法
Oct 26 Python
python接口自动化(十七)--Json 数据处理---一次爬坑记(详解)
Apr 18 Python
numpy.linspace函数具体使用详解
May 27 Python
Python二进制文件读取并转换为浮点数详解
Jun 25 Python
python 日期排序的实例代码
Jul 11 Python
Python编写通讯录通过数据库存储实现模糊查询功能
Jul 18 Python
django admin 自定义替换change页面模板的方法
Aug 23 Python
Python可变参数会自动填充前面的默认同名参数实例
Nov 18 Python
python 办公自动化——基于pyqt5和openpyxl统计符合要求的名单
May 25 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中的Class的几点个人看法
2006/10/09 PHP
PHP 读取文件的正确方法
2009/04/29 PHP
PHP setTime 设置当前时间的代码
2012/08/27 PHP
仅用[]()+!等符号就足以实现几乎任意Javascript代码
2010/03/01 Javascript
autoPlay 基于jquery的图片自动播放效果
2011/12/07 Javascript
利用JS进行图片的切换即特效展示图片
2013/12/03 Javascript
jquery常用操作小结
2014/07/21 Javascript
零基础搭建Node.js、Express、Ejs、Mongodb服务器及应用开发入门
2014/12/20 Javascript
thinkphp实现无限分类(使用递归)
2015/12/19 Javascript
js无法获取到html标签的属性的解决方法
2016/07/26 Javascript
提高JavaScript执行效率的23个实用技巧
2017/03/01 Javascript
AngularJs实现聊天列表实时刷新功能
2017/06/15 Javascript
微信小程序 websocket 实现SpringMVC+Spring+Mybatis
2017/08/04 Javascript
vue 页面加载进度条组件实例
2018/02/05 Javascript
详解用Node.js写一个简单的命令行工具
2018/03/01 Javascript
layui实现鼠标移动到单元格上显示数据的方法
2019/09/11 Javascript
vue实现购物车案例
2020/05/30 Javascript
Python的Flask框架中SQLAlchemy使用时的乱码问题解决
2015/11/07 Python
浅谈Python的异常处理
2016/06/19 Python
Python操作mysql数据库实现增删查改功能的方法
2018/01/15 Python
python使用magic模块进行文件类型识别方法
2018/12/08 Python
python使用正则筛选信用卡
2019/01/27 Python
Python的垃圾回收机制详解
2019/08/28 Python
python中自带的三个装饰器的实现
2019/11/08 Python
Python 使用threading+Queue实现线程池示例
2019/12/21 Python
Django调用支付宝接口代码实例详解
2020/04/04 Python
Python通过format函数格式化显示值
2020/10/17 Python
基于zepto的插件之移动端无缝向上滚动并上下触摸滑动实例代码
2016/12/20 HTML / CSS
Hotter Shoes英国官网:英伦风格,舒适的鞋子
2017/12/28 全球购物
马来西亚综合购物网站:Lazada马来西亚
2018/06/05 全球购物
德国健康生活方式网上商店:Landkaufhaus Mayer
2019/03/12 全球购物
机械工程师的岗位职责
2013/11/17 职场文书
建筑施工员岗位职责
2013/11/26 职场文书
html实现弹窗的实例
2021/06/09 HTML / CSS
图解排序算法之希尔排序Java实现
2021/06/26 Java/Android
Vue OpenLayer测距功能的实现
2022/04/20 Vue.js