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中变量赋值的问题
Jan 12 Python
python条件变量之生产者与消费者操作实例分析
Mar 22 Python
简述:我为什么选择Python而不是Matlab和R语言
Nov 14 Python
Python实现合并同一个文件夹下所有txt文件的方法示例
Apr 26 Python
使用anaconda的pip安装第三方python包的操作步骤
Jun 11 Python
Python登录注册验证功能实现
Jun 18 Python
python自动发送邮件脚本
Jun 20 Python
python集合比较(交集,并集,差集)方法详解
Sep 13 Python
python 文本单词提取和词频统计的实例
Dec 22 Python
python 批量解压压缩文件的实例代码
Jun 27 Python
python在新的图片窗口显示图片(图像)的方法
Jul 11 Python
Django import export实现数据库导入导出方式
Apr 03 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动态生成VRML网页
2006/10/09 PHP
修改ThinkPHP缓存为Memcache的方法
2014/06/25 PHP
php读取远程gzip压缩网页的方法
2014/12/29 PHP
memcache一致性hash的php实现方法
2015/03/05 PHP
Linux+Nginx+MySQL下配置论坛程序Discuz的基本教程
2015/12/23 PHP
PHP实现163邮箱自动发送邮件
2016/03/29 PHP
CI框架整合widget(页面格局)的方法
2016/05/17 PHP
PHP单例模式与工厂模式详解
2017/08/29 PHP
ThinkPHP5.0多个文件上传后找不到临时文件的修改方法
2018/07/30 PHP
PHP 观察者模式深入理解与应用分析
2019/09/25 PHP
基于laravel Request的所有方法详解
2019/09/29 PHP
基于node.js的快速开发透明代理
2010/12/25 Javascript
更快的异步执行(setTimeout多浏览器)
2014/08/12 Javascript
Node.JS更改Windows注册表Regedit的方法小结
2017/08/18 Javascript
深入解读Node.js中的koa源码
2019/06/17 Javascript
小程序按钮避免多次调用接口和点击方案实现(不用showLoading)
2020/04/15 Javascript
vue 项目@change多个参数传值多个事件的操作
2021/01/29 Vue.js
用Python进行一些简单的自然语言处理的教程
2015/03/31 Python
Python中asyncore异步模块的用法及实现httpclient的实例
2016/06/28 Python
Python 中PyQt5 点击主窗口弹出另一个窗口的实现方法
2019/07/04 Python
Python通过Schema实现数据验证方式
2020/11/12 Python
css3实现文字首尾衔接跑马灯的示例代码
2020/10/16 HTML / CSS
Turnbull & Asser官网:英国皇室御用的顶级定制衬衫
2019/01/31 全球购物
简单叙述一下MYSQL的优化
2016/05/09 面试题
行政专员求职信范文
2014/05/03 职场文书
信用社主任竞聘演讲稿
2014/05/23 职场文书
环境卫生标语
2014/06/09 职场文书
日语专业毕业生自荐书
2014/06/18 职场文书
项目合作意向书模板
2014/07/29 职场文书
户籍证明格式
2014/09/15 职场文书
抢劫罪辩护词
2015/05/21 职场文书
家装电话营销开场白
2015/05/29 职场文书
导游词之南京中山陵
2019/11/27 职场文书
python实现监听键盘
2021/04/26 Python
html5实现点击弹出图片功能
2021/07/16 HTML / CSS
python绘制云雨图raincloud plot
2022/08/05 Python