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 教师,我发现理解装饰器是学生 ...
随机推荐
- java集合Map
参考文章:https://blog.csdn.net/yjn1995/article/details/89784891 1.map接口 1.map接口实现类,HashMap.LinkListMap.H ...
- [Javascript] Avoid Accidental Returns of New State by using the void Keyword
For example we have a 'useState' function, which takes a state and a function to update the state: c ...
- URLSearchParams对象
URLSearchParams对象用于处理URL中查询字符串,即?之后的部分. 1.语法 其实例对象的用法和Set数据结构类似.实例对象本身是可遍历对象.但是不是遍历器. var paramsStri ...
- DBA 有哪些工作
首先,我们看看DBA的工作有哪些?DBA的工作实际上都是围绕数据库展开,包含但不限于这些工作: 1. 数据库.主机.操作系统.交换机.存储选型,预算,架构设计,部署,参数优化: 2. 数据库备份.恢复 ...
- 021_Python3 OS 文件/目录方法
os 模块提供了非常丰富的方法用来处理文件和目录.常用的方法如下表所示: 序号 方法及描述 1 os.access(path, mode) 检验权限模式 2 os.chdir(path) 改变当前 ...
- HTML5全屏操作API
一.定义:HTML5规范允许自定义网页上的任一元素全屏显示,存在兼容问题 二.使用: ①基本: Node.RequestFullScreen()开启全屏显示 Node.CancelFullScreen ...
- linux的计划任务操作
1.cron服务来设置 计划任务查看与设置命令:crontab 包括条目: 分钟m:0-59 小时h:0-23 月日dom:1-31 月份mon:1-12 星期dow:0-7 例子: 每隔2小时处理一 ...
- jackson实现json转换
第一步.导包.导入jar包或者在maven项目中导入坐标(jackson-annotations:jackson-core:jackson-databind) 第二步.通过无参构造创建核心类Objec ...
- EasyTrader踩坑之旅(三)
快速阅读 用THSTrader 调试同花顺自动下单的过程 . 主要原理是利用python函数pywinauto 自动获取同花顺上相应控件的值,进行模拟自动化的操作,不得不说python函数库的强大 ...
- linux xlearn安装
机器学习中的又一个利器,广泛用于Kaggle或类似的数据比赛. xlearn的优势: 1.通用性好,包括主流的算法(lr, fm, ffm 等),用户不用再切换于不同软件之间 2.性能好,测试 xL ...