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 str与repr的区别
Mar 23 Python
python实现文件名批量替换和内容替换
Mar 20 Python
使用python实现拉钩网上的FizzBuzzWhizz问题示例
May 05 Python
Python编写百度贴吧的简单爬虫
Apr 02 Python
python虚拟环境virtualenv的安装与使用
Sep 21 Python
python实现求最长回文子串长度
Jan 22 Python
Python操作Excel工作簿的示例代码(\*.xlsx)
Mar 23 Python
Jupyter打开图形界面并画出正弦函数图像实例
Apr 24 Python
Python如何使用ElementTree解析xml
Oct 12 Python
Python实现迪杰斯特拉算法并生成最短路径的示例代码
Dec 01 Python
pandas中DataFrame数据合并连接(merge、join、concat)
May 30 Python
python树莓派通过队列实现进程交互的程序分析
Jul 04 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
ThinkPHP查询返回简单字段数组的方法
2014/08/25 PHP
php简单实现快速排序的方法
2015/04/04 PHP
php防止用户重复提交表单
2015/11/02 PHP
解析PHP的Yii框架中cookie和session功能的相关操作
2016/03/17 PHP
CI框架的安全性分析
2016/05/18 PHP
php打乱数组二维数组多维数组的简单实例
2016/06/17 PHP
Add a Picture to a Microsoft Word Document
2007/06/15 Javascript
JQuery Dialog的内存泄露问题解决方法
2010/06/18 Javascript
jQuery添加/改变/移除CSS类及判断是否已经存在CSS
2014/08/20 Javascript
jQuery中append()方法用法实例
2014/12/25 Javascript
JQuery中使文本框获得焦点的方法实例分析
2015/02/28 Javascript
jQuery 获取多选框的值及多选框中文的函数
2016/05/16 Javascript
JS &amp; JQuery 动态添加 select option
2016/06/08 Javascript
jQuery实现点击行选中或取消CheckBox的方法
2016/08/01 Javascript
常用js,css文件统一加载方法(推荐) 并在加载之后调用回调函数
2016/09/23 Javascript
jquery鼠标悬停导航下划线滑出效果
2017/09/29 jQuery
JS实现遍历不规则多维数组的方法
2018/03/21 Javascript
详解webpack模块化管理和打包工具
2018/04/21 Javascript
angular4 获取wifi列表中文显示乱码问题的解决
2018/10/20 Javascript
Vue项目结合Vue-layer实现弹框式编辑功能(实例代码)
2020/03/11 Javascript
Ant-design-vue Table组件customRow属性的使用说明
2020/10/28 Javascript
[57:53]DOTA2上海特级锦标赛主赛事日 - 2 败者组第二轮#3OG VS VP
2016/03/03 DOTA
[01:07:17]EG vs Optic Supermajor 败者组 BO3 第一场 6.6
2018/06/07 DOTA
Python多线程实现同步的四种方式
2017/05/02 Python
Python提取特定时间段内数据的方法实例
2019/04/01 Python
用Python中的turtle模块画图两只小羊方法
2019/04/09 Python
python程序文件扩展名知识点详解
2020/02/27 Python
如何在Python 游戏中模拟引力
2020/03/27 Python
python利用paramiko实现交换机巡检的示例
2020/09/22 Python
python实现马丁策略的实例详解
2021/01/15 Python
HTML中meta标签及Keywords
2020/04/15 HTML / CSS
HTML5 body设置自适应全屏
2020/05/07 HTML / CSS
中学生社会实践教育活动总结
2015/05/06 职场文书
2019中秋节祝福语大全,提前收藏啦
2019/09/10 职场文书
nginx刷新页面出现404解决方案(亲测有效)
2022/03/18 Servers
python​格式化字符串
2022/04/20 Python