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快速排序代码实例
Nov 21 Python
10种检测Python程序运行时间、CPU和内存占用的方法
Apr 01 Python
在Windows系统上搭建Nginx+Python+MySQL环境的教程
Dec 25 Python
Python3实现的简单三级菜单功能示例
Mar 12 Python
python读取指定字节长度的文本方法
Aug 27 Python
在Pytorch中计算自己模型的FLOPs方式
Dec 30 Python
pytorch 实现cross entropy损失函数计算方式
Jan 02 Python
解决Python命令行下退格,删除,方向键乱码(亲测有效)
Jan 16 Python
Python编程快速上手——选择性拷贝操作案例分析
Feb 28 Python
django rest framework 自定义返回方式
Jul 12 Python
Python如何设置指定窗口为前台活动窗口
Aug 12 Python
anaconda python3.8安装后降级
Jun 11 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 默默经典版本
2009/08/04 PHP
一篇有意思的技术文章php介绍篇
2010/10/26 PHP
js querySelector和getElementById通过id获取元素的区别
2012/04/20 Javascript
通过$(this)使用jQuery包装后的方法或属性
2014/05/18 Javascript
jQuery实现复选框全选/取消全选/反选及获得选择的值
2014/06/12 Javascript
TypeScript具有的几个不同特质
2015/04/07 Javascript
基于Jquery代码实现支持PC端手机端幻灯片代码
2015/11/17 Javascript
详解JavaScript中常用的函数类型
2015/11/18 Javascript
javascript编程异常处理实例小结
2015/11/30 Javascript
jQuery图片旋转插件jQueryRotate.js用法实例(附demo下载)
2016/01/21 Javascript
javascript+HTML5自定义元素播放焦点图动画
2016/02/21 Javascript
浅谈javascript中的三种弹窗
2016/10/21 Javascript
Vue.js手风琴菜单组件开发实例
2017/05/16 Javascript
vue-cli单页应用改成多页应用配置详解
2017/07/14 Javascript
操作按钮悬浮固定在微信小程序底部的实现代码
2019/08/02 Javascript
Javascript原生ajax请求代码实例
2020/02/20 Javascript
[51:36]Optic vs Newbee 2018国际邀请赛小组赛BO2 第一场 8.17
2018/08/18 DOTA
python使用PythonMagick将jpg图片转换成ico图片的方法
2015/03/26 Python
带你了解python装饰器
2017/06/15 Python
Python进阶之递归函数的用法及其示例
2018/01/31 Python
flask入门之文件上传与邮件发送示例
2018/07/18 Python
pandas通过字典生成dataframe的方法步骤
2019/07/23 Python
Python的条件锁与事件共享详解
2019/09/12 Python
python 实现两个线程交替执行
2020/05/02 Python
Html5画布_动力节点Java学院整理
2017/07/13 HTML / CSS
HTML5语义化元素你真的用对了吗
2019/08/22 HTML / CSS
Right-on官方网站:日本知名的休闲服装品牌
2019/07/12 全球购物
巴西Mr. Cat在线商店:购买包包和鞋子
2019/09/08 全球购物
爱心活动计划书
2014/04/26 职场文书
员工趣味活动方案
2014/08/27 职场文书
农村环境卫生倡议书
2015/04/29 职场文书
工会经费申请报告
2015/05/15 职场文书
redis三种高可用方式部署的实现
2021/05/11 Redis
手把手教你使用TensorFlow2实现RNN
2021/07/15 Python
SQL之各种join小结详细讲解
2021/08/04 MySQL
Python实现Excel文件的合并(以新冠疫情数据为例)
2022/03/20 Python