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中用get()方法获取字典键值的教程
May 21 Python
python使用reportlab实现图片转换成pdf的方法
May 22 Python
举例讲解Python设计模式编程的代理模式与抽象工厂模式
Jan 16 Python
Python变量和字符串详解
Apr 29 Python
Python操作MongoDB数据库的方法示例
Jan 04 Python
python 信息同时输出到控制台与文件的实例讲解
May 11 Python
使用50行Python代码从零开始实现一个AI平衡小游戏
Nov 21 Python
PyCharm搭建Spark开发环境的实现步骤
Sep 05 Python
基于Python中isfile函数和isdir函数使用详解
Nov 29 Python
Python实现猜年龄游戏代码实例
Mar 25 Python
python 中[0]*2与0*2的区别说明
May 10 Python
python tkinter Entry控件的焦点移动操作
May 22 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
phpQuery占用内存过多的处理方法
2013/11/13 PHP
javascript数组与php数组的地址传递及值传递用法实例
2015/01/22 PHP
php筛选不存在的图片资源
2015/04/28 PHP
php返回当前日期或者指定日期是周几
2015/05/21 PHP
浅谈PHP中的Trait使用方法
2019/03/22 PHP
在laravel框架中实现封装公共方法全局调用
2019/10/14 PHP
jQuery的显示和隐藏方法与css隐藏的样式对比
2013/10/18 Javascript
JS将制定内容复制到剪切板示例代码
2014/02/11 Javascript
jQuery中children()方法用法实例
2015/01/07 Javascript
修改jquery中dialog的title属性方法(推荐)
2016/08/26 Javascript
javascript中的深复制详解及实例分析
2016/12/29 Javascript
js实现华丽的九九乘法表效果
2017/03/29 Javascript
详解基于webpack搭建react运行环境
2017/06/01 Javascript
详解在vue-cli项目中使用mockjs(请求数据删除数据)
2017/10/23 Javascript
浅谈Node.js 子进程与应用场景
2018/01/24 Javascript
vue项目中使用百度地图的方法
2018/06/08 Javascript
Element-UI中关于table表格的那些骚操作(小结)
2019/08/15 Javascript
微信小程序开发中var that =this的用法详解
2020/01/18 Javascript
详解Webpack抽离第三方类库以及common解决方案
2020/03/30 Javascript
从表单校验看JavaScript策略模式的使用详解
2020/10/17 Javascript
如何使用Python破解ZIP或RAR压缩文件密码
2020/01/09 Python
python编程进阶之异常处理用法实例分析
2020/02/21 Python
Python实现AI换脸功能
2020/04/10 Python
pandas数据拼接的实现示例
2020/04/16 Python
Python列表去重复项的N种方法(实例代码)
2020/05/12 Python
使用Nibabel库对nii格式图像的读写操作
2020/07/01 Python
基于CentOS搭建Python Django环境过程解析
2020/08/24 Python
python3代码输出嵌套式对象实例详解
2020/12/03 Python
哈萨克斯坦最大的时装、鞋子和配饰在线商店:Lamoda.kz
2019/11/19 全球购物
世界上最大的铁人三项商店:Tri UK
2020/11/04 全球购物
static关键字的用法
2013/10/07 面试题
教师先进个人材料
2014/12/17 职场文书
工程部主管岗位职责
2015/02/12 职场文书
项目负责人岗位职责
2015/02/15 职场文书
Nginx搭建rtmp直播服务器实现代码
2021/03/31 Servers
MySQL连表查询分组去重的实现示例
2021/07/01 MySQL