Django视图函数函数之视图装饰器
FBV模式装饰器:
普通函数的装饰器(语法糖@)
views.py
from django.shortcuts import render def wrapper(f):
def inner(*args,**kwargs):
print("before")
ret=f(*args,**kwargs)
print("after")
return ret
return inner @wrapper
def index(request):
return render(request,"index.html")
CBV模式装饰器:
在CBV模式视图函数中必须先导入:from django.views import View
(1)重写父类dispatch分发方法,在分发执行每个请求响应函数前后加上相应功能为实现类比装饰器
views.py
from django.shortcuts import render,HttpResponse
from django.views import View
from django.utils.decorators import method_decorator class Myview(View): def dispatch(self, request, *args, **kwargs):
print("before")
ret=super().dispatch(request, *args, **kwargs)
print("after")
return ret def get(self, request):
return render(request, "login.html") def post(self, request):
if request.method == "GET":
return render(request, "login.html")
elif request.method == "POST":
if request.POST.get("username") == "yang" and request.POST.get("userpsd") == "":
return render(request, "login_success.html", {"name": request.POST.get("username")})
else:
return HttpResponse("账号或密码有误!")
(2)在子类重写分发函数时加上装饰器(每个请求函数都会被装饰)
必须先导入:
from django.views import View
from django.utils.decorators import method_decorator
views.py
from django.shortcuts import render,HttpResponse
from django.views import View
from django.utils.decorators import method_decorator def wrapper(f):
def inner(*args,**kwargs):
print("before")
ret=f(*args,**kwargs)
print("after")
return ret
return inner class Myview(View): @method_decorator(wrapper)
def dispatch(self, request, *args, **kwargs):
ret=super().dispatch(request, *args, **kwargs)
return ret def get(self, request):
return render(request, "login.html") def post(self, request):
if request.method == "GET":
return render(request, "login.html")
elif request.method == "POST":
if request.POST.get("username") == "yang" and request.POST.get("userpsd") == "":
return render(request, "login_success.html", {"name": request.POST.get("username")})
else:
return HttpResponse("账号或密码有误!")
(3)在子类重写的不同响应请求函数上加上装饰器
必须先导入:
from django.views import View
from django.utils.decorators import method_decorator
views.py
from django.shortcuts import render,HttpResponse
from django.views import View
from django.utils.decorators import method_decorator def wrapper(f):
def inner(*args,**kwargs):
print("before")
ret=f(*args,**kwargs)
print("after")
return ret
return inner class Myview(View):
# def dispatch(self, request, *args, **kwargs):
# ret=super().dispatch(request, *args, **kwargs)
# return ret def get(self, request):
return render(request, "login.html") @method_decorator(wrapper)
def post(self, request):
if request.method == "GET":
return render(request, "login.html")
elif request.method == "POST":
if request.POST.get("username") == "yang" and request.POST.get("userpsd") == "":
return render(request, "login_success.html", {"name": request.POST.get("username")})
else:
return HttpResponse("账号或密码有误!")
(4)在子类定义时加上装饰器,必须指定而且唯一指定加在的函数
必须先导入:
from django.views import View
from django.utils.decorators import method_decorator
views.py
from django.shortcuts import render,HttpResponse
from django.views import View
from django.utils.decorators import method_decorator def wrapper(f):
def inner(*args,**kwargs):
print("before")
ret=f(*args,**kwargs)
print("after")
return ret
return inner @method_decorator(wrapper,name="post") class Myview(View):
# def dispatch(self, request, *args, **kwargs):
# ret=super().dispatch(request, *args, **kwargs)
# return ret def get(self, request):
return render(request, "login.html") def post(self, request):
if request.method == "GET":
return render(request, "login.html")
elif request.method == "POST":
if request.POST.get("username") == "yang" and request.POST.get("userpsd") == "":
return render(request, "login_success.html", {"name": request.POST.get("username")})
else:
return HttpResponse("账号或密码有误!")
其它装饰器:
· 添加装饰器前必须导入from django.utils.decorators import method_decorator
· 添加装饰器的格式必须为@method_decorator(),括号里面为装饰器的函数名
· 给类添加是必须声明name
· 注意csrf-token装饰器的特殊性,在CBV模式下它只能加在dispatch上面(后面再说)
下面这是csrf_token的装饰器:
@csrf_protect,为当前函数强制设置防跨站请求伪造功能,即便settings中没有设置csrfToken全局中间件。
@csrf_exempt,取消当前函数防跨站请求伪造功能,即便settings中设置了全局中间件。
注意:from django.views.decorators.csrf import csrf_exempt,csrf_protect
Django视图函数函数之视图装饰器的更多相关文章
- python描述符(descriptor)、属性(property)、函数(类)装饰器(decorator )原理实例详解
1.前言 Python的描述符是接触到Python核心编程中一个比较难以理解的内容,自己在学习的过程中也遇到过很多的疑惑,通过google和阅读源码,现将自己的理解和心得记录下来,也为正在为了该问题 ...
- Python的函数式编程-传入函数、排序算法、函数作为返回值、匿名函数、偏函数、装饰器
函数是Python内建支持的一种封装,我们通过把大段代码拆成函数,通过一层一层的函数调用,就可以把复杂任务分解成简单的任务,这种分解可以称之为面向过程的程序设计.函数就是面向过程的程序设计的基本单元. ...
- Python【第四篇】函数、内置函数、递归、装饰器、生成器和迭代器
一.函数 函数是指将一组语句的集合通过一个名字(函数名)封装起来,要想执行这个函数,只需调用其函数名即可 特性: 减少重复代码 使程序变的可扩展 使程序变得易维护 1.定义 def 函数名(参数): ...
- python 函数名 、闭包 装饰器 day13
1,函数名的使用. 函数名是函数的名字,本质就是变量,特殊的变量.函数名()加括号就是执行此函数. 1,单独打印函数名就是此函数的内存地址. def func1(): print(555) print ...
- python 全栈开发,Day11(函数名应用,闭包,装饰器初识,带参数以及带返回值的装饰器)
一.函数名应用 函数名是什么?函数名是函数的名字,本质:变量,特殊的变量. 函数名(),执行此函数. python 规范写法 1. #后面加一个空格,再写内容,就没有波浪线了. 2.一行代码写完,下面 ...
- Python 函数修饰符(装饰器)的使用
Python 函数修饰符(装饰器)的使用 1. 修饰符的来源修饰符是一个很著名的设计模式,经常被用于有切面需求的场景,较为经典的有插入日志.性能测试.事务处理等. 修饰符是解决这类问题的绝佳设计, ...
- Python之路【第五篇】: 函数、闭包、装饰器、迭代器、生成器
目录 函数补充进阶 函数对象 函数的嵌套 名称空间与作用域 闭包函数 函数之装饰器 函数之可迭代对象 函数之迭代器 函数之生成器 面向过程的程序设计思想 一.函数进阶之函数对象 1. 函数对象 秉承着 ...
- 第十七篇 Python函数之闭包与装饰器
一. 装饰器 装饰器:可以拆解来看,器本质就是函数,装饰就是修饰的意思,所以装饰器的功能就是为其他函数添加附加功能. 装饰器的两个原则: 1. 不修改被修饰函数的源代码 2. 不修改被修饰函数的调用方 ...
- Day 19 函数之闭包、装饰器
一.什么是装饰器 器即函数 装饰即修饰,意指为其他函数添加新功能 装饰器定义:本质就是函数,功能是为其他函数添加新功能 二.装饰器遵循的原则 1.不修改被装饰函数的源代码(开放封闭原则) 2.为被装饰 ...
- 装饰器带类参数 & 一个函数应用多个装饰器
装饰器:不改变原函数的基础上,给函数增加功能的方式,称为装饰器 即:为已经存在的对象添加额外的功能 装饰器其实就是一个闭包,把一个函数当做参数后返回一个替代版的函数 decos.py:(装饰器的参数类 ...
随机推荐
- php内置函数call_user_func()
<?php //call_user_func(callback,name,age) //第一个参数callback作为回掉函数使用,其余的参数是他的参数 function now($a,$b) ...
- 2019-2020-1 20199308《Linux内核原理与分析》第四周作业
<Linux内核分析> 第三章 MenuOS的构造 3.1 Linux内核源代码简介 操作系统的"两把宝剑" 中断上下文:保存现场和恢复现场 进程上下文 目录结构 ar ...
- python学习22之函数式编程
'''''''''1.高阶函数:将函数作为参数传递到另一个函数中,作为这个函数的参数,这样的方式叫高阶函数(1)map:两个参数,一个是函数,一个是iterator,将函数依次作用于Iterator中 ...
- union 的概念及在嵌入式编程中的应用
union 概念 union 在中文的叫法中又被称为共用体,联合或者联合体,它定义的方式与 struct 是相同的,但是意义却与 struct 完全不同,下面是 union 的定义格式: union ...
- Imagelab-0-QT label显示 opencv 图像
Imagelab-0-QT label显示 opencv 图像 opencvc++qtimagelab 开始之前 这其实也是opencv 处理图像的系列, 只是想我们在进一步复杂化我们的代码之前, 每 ...
- Libra教程之:Transaction的生命周期
文章目录 Transaction的生命周期 提交一个Transaction 交易入链的详细过程 接收Transaction 和其他Validators共享这个Transaction 区块Proposi ...
- 3年前的一个小项目经验,分享给菜鸟兄弟们(公文收发小软件:小技能 SmallDatetime)...
为什么80%的码农都做不了架构师?>>> 这个系统中的数据库有100多M,里面当然有很多表,我的每个表里,有几个字段,都是一样的例如 CreateUserID.CreateDat ...
- 如何设计高并发web应用
所谓高并发,就是同一时间有很多流量(通常指用户)访问程序的接口.页面及其他资源,解决高并发就是当流量峰值到来时保证程序的稳定性. 我们一般用QPS(每秒查询数,又叫每秒请求数)来衡量程序的综合性能 ...
- js的同步与异步
JavaScript语言的一大特点就是单线程,也就是说,同一个时间只能做一件事.那么,为什么JavaScript不能有多个线程呢?这样能提高效率啊. JavaScript的单线程,与它的用途有关.作为 ...
- du命令、df命令、磁盘分区
df:汇报文件系统的磁盘使用空间[root@localhost ~]# df文件系统 1K-块 已用 可用 已用% 挂载点/dev/sda3 29140072 1022920 28117152 4% ...