Django框架用户注销功能实现方法分析

作者:学习笔记666 时间:2021-07-22 17:58:08 

本文实例讲述了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程序设计有所帮助。

来源:https://blog.csdn.net/github_26672553/article/details/52497045

标签:Django,用户注销
0
投稿

猜你喜欢

  • python自动化测试之从命令行运行测试用例with verbosity

    2021-09-20 07:32:16
  • 如何给eWebEditor编辑器加上运行代码框功能

    2007-09-25 07:02:00
  • JSON.stringify转换JSON时日期时间不准确的解决方法

    2014-07-20 13:25:07
  • 基于OpenCV的PHP图像人脸识别技术

    2023-11-23 22:02:54
  • 利用box-sizing实现div仿框架

    2009-12-08 15:45:00
  • 验证sql保留字工具

    2008-05-15 12:34:00
  • 基于PHP+MySQL的聊天室设计

    2023-11-22 19:26:25
  • php 使用 __call实现重载功能示例

    2023-07-13 20:10:28
  • python保存图片时如何和原图大小一致

    2022-07-13 03:34:36
  • asp error对象基础

    2008-08-04 13:25:00
  • 合理关闭XHTML标签

    2008-06-25 13:20:00
  • jQuery在去除缓存数据的一个失误

    2009-12-14 20:40:00
  • ThinkPHP基于think-queue的队列插件实现消息推送

    2023-05-25 05:59:12
  • Bootstrap响应式侧边栏改进版

    2023-08-17 02:26:10
  • 有效地使用 SQL事件探查器的提示和技巧

    2009-01-15 13:39:00
  • Python编程中的反模式实例分析

    2021-02-01 09:54:26
  • 一个修改Oracle数据库用户密码的小诀窍

    2009-09-30 15:29:00
  • Google的设计导引

    2008-04-06 14:18:00
  • Dreamweaver使用快技法十三则

    2009-07-21 12:45:00
  • 写给应聘页面重构的同学

    2009-03-18 11:01:00
  • asp之家 网络编程 m.aspxhome.com