Python - Django - 装饰器版的登陆校验
urls.py:
from django.conf.urls import url
from app01 import views urlpatterns = [
url(r'^login/', views.login),
url(r'^home/', views.home),
url(r'^index/', views.index),
url(r'^logout/', views.logout),
]
views.py:
from django.shortcuts import render, redirect
from app01 import models from functools import wraps # 登录校验的装饰器
def check_login(func):
@wraps(func) # 装饰器修复技术
def inner(request, *args, **kwargs):
ret = request.get_signed_cookie("login", default="0", salt="whoami")
if ret == "success":
# 已经登录过,继续执行
return func(request, *args, **kwargs)
else:
# 否则跳转到登录页面
next_url = request.path_info # 获取当前访问的 URL
# next_url = request.get_full_path() # 获取当前请求的路径和参数
return redirect("/login/?next={}".format(next_url))
return inner def login(request):
if request.method == "POST":
username = request.POST.get("user")
password = request.POST.get("pwd")
next_url = request.GET.get("next") if username == "admin" and password == "admin":
if next_url:
rep = redirect(next_url) # 得到一个响应对象
else:
rep = redirect("/home/") # 得到一个响应对象
# rep.set_cookie("login", "success") # 设置 cookie
rep.set_signed_cookie("login", "success", salt="whoami") # 设置 cookie 并加盐
return rep ret = request.get_signed_cookie("login", default="0", salt="whoami")
if ret == "success":
return redirect("/home/") # 如果已经登录过,再访问 login,直接跳转到 home
else:
return render(request, "login.html") def home(request):
# ret = request.COOKIES.get("login") # 获取 cookie 的 value
ret = request.get_signed_cookie("login", default="0", salt="whoami") # 获取加盐后 cookie 的 value
if ret == "success":
# cookie 验证成功
return render(request, "home.html")
else:
return redirect("/login/") @check_login
def index(request):
return render(request, "index.html") # 注销函数
def logout(request):
rep = redirect("/login/")
rep.delete_cookie("login") # 删除 cookie
return rep
login.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登录页面</title>
</head>
<body> <p>登录页面</p> <form action="{{ request.get_full_path }}" method="post">
{% csrf_token %}
<p>
账号:
<input type="text" name="user">
</p>
<p>
密码:
<input type="text" name="pwd">
</p>
<p>
<input type="submit" value="登录">
</p>
</form> </body>
</html>
home.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>个人信息页面</title>
</head>
<body> <p>个人信息页面</p> <a href="/logout/">注销</a> </body>
</html>
index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>主页面</title>
</head>
<body> <p>主页面</p> </body>
</html>
访问,http://127.0.0.1:8888/index/

输入,admin、admin

直接跳转到了 index 页面
这时再访问 login 页面,就会跳转到 home 页面

点击 “注销”

回到了登录界面
Python - Django - 装饰器版的登陆校验的更多相关文章
- Python练习-装饰器版-为什么我的用户总被锁定
参考代码如下: 1.用户登录程序流程控制代码: # 编辑者:闫龙 if __name__ == '__main__': import UserLoginFuncation LoclCount=[]; ...
- 浅谈Django的中间件与Python的装饰器
浅谈Django的中间件 与Python的装饰器 一.原理 1.装饰器是Python的一种语法应用,利用闭包的原理去更改一个函数的功能,即让一个函数执行之前先到另外一个函数中执行其他需求语句,在执行该 ...
- django 使用装饰器验证用户登陆
使用装饰器验证用户登陆,需要使用@method_decorator 首先需引用,method_decorator,并定义一个闭包 from django.utils.decorators import ...
- Python的装饰器实例用法小结
这篇文章主要介绍了Python装饰器用法,结合实例形式总结分析了Python常用装饰器的概念.功能.使用方法及相关注意事项 一.装饰器是什么 python的装饰器本质上是一个Python函数,它可以让 ...
- 【转】详解Python的装饰器
原文链接:http://python.jobbole.com/86717/ Python中的装饰器是你进入Python大门的一道坎,不管你跨不跨过去它都在那里. 为什么需要装饰器 我们假设你的程序实现 ...
- 详解Python的装饰器
Python中的装饰器是你进入Python大门的一道坎,不管你跨不跨过去它都在那里. 为什么需要装饰器 我们假设你的程序实现了say_hello()和say_goodbye()两个函数. def sa ...
- 【转】【Python】装饰器
1.闭包 >>> def outer(): ... x = 1 ... def inner(): ... ... return inner >>> foo = ou ...
- Python学习---装饰器的学习1210
装饰器的基础 学习前提: 作用域 + 函数的理解 + 闭包 [学习,理解] 代码编写原则: 对修改开放对扩展开放 装饰器本质上是一个函数,该函数用来处理其他函数,它可以让其他函数在不需要修改代码的前 ...
- Python中装饰器(转)
本文由 伯乐在线 - 7even 翻译,艾凌风 校稿.未经许可,禁止转载!英文出处:Simeon Franklin.欢迎加入翻译组. 好吧,我标题党了.作为 Python 教师,我发现理解装饰器是学生 ...
随机推荐
- RCNN,Fast RCNN,Faster RCNN 的前生今世:(4) Fast RCNN 算法详解
继2014年的RCNN之后,Ross Girshick在15年推出Fast RCNN,构思精巧,流程更为紧凑,大幅提升了目标检测的速度.在Github上提供了源码. 同样使用最大规模的网络,Fast ...
- mysql跨表删除多条记录
Mysql可以在一个sql语句中同时删除多表记录,也可以根据多个表之间的关系来删除某一个表中的记录. 假定我们有两张表:Product表和ProductPrice表.前者存在Product的基本信息, ...
- STAF Trust Level 4 required for FS copy request
C#中使用STAF从本机传输文件到远程的电脑,出现如下错误: 解决方法: 修改service.ini文件,添加信任IP段,并将trust level 设置为5,修改后文件内容如下 trace enab ...
- Kafka+kylin——kylin2.5.0流式构建
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/a_drjiaoda/article/d ...
- Web API系列(四) 使用ActionFilterAttribute 记录 WebApi Action 请求和返回结果记录
转自:https://www.cnblogs.com/hnsongbiao/p/7039666.html 需要demo在github中下载: https://github.com/shan333cha ...
- GitHub上的一些使用技巧
1.搜索 转:掌握 3 个搜索技巧,在 GitHub 上快速找到实用软件资源 例如 查找位于深圳的C#开发者 2.查看文件历史提交记录 定位至需要查看的文件 修改地址栏github.com 为 git ...
- Firefox修復QQ快速登錄
中了一次毒,然後火狐裏面就不能用QQ的快捷登錄了,後找到修復方法: 將QQ的四個文件放入火狐的插件文件夾裏面即可. 1.QQ文件目錄: C:\Program Files (x86)\Tencent\Q ...
- Leetcode44. 通配符匹配(动态规划)
44. 通配符匹配 动态规划 \(f_{i,j}\)为\(s\)匹配\(i\),\(t\)匹配\(j\)是否成功 贪心 相比之下这个思维性更强 考虑两个*,两个星号间的过渡,只需要过渡完到第二个星号, ...
- OpenFOAM当中物性参数的设置
固体当中物性参数的设置: FoamFile { version 2.0; format ascii; class dictionary; object thermophysicalProperties ...
- JDBC 操作
简单的 JDBC 操作主要有: JdbcTemplate query queryForObject queryForList update execute 简单使用如下所示. 初始化数据库 sprin ...