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下载各种编辑器输出的内容到word中展示
<?php/** * Created by PhpStorm. * User: 工作 * Date: 2018/1/11 * Time: 12:02 */ //连接数据库$dsn = " ...
- centos7下端口映射
firewall-cmd --zone=external --add-forward-port=port=:proto=tcp:toport=:toaddr=192.168.10.10 --perma ...
- typeahead自动补全插件的limit参数问题
遇到的问题很诡异: 后台返回的数据都正确就是显示不正常(有时多有时少),后来发现是typeahead的问题,在1.11版本之后,limit参数从option选项里改到了setdata选项: limit ...
- Python 如何实现 单实例
出处:https://stackoverflow.com/questions/380870/make-sure-only-a-single-instance-of-a-program-is-runni ...
- [Windows] Diskpart Scripts and Examples
https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/diskpart-scripts-and ...
- 数学--博弈论--巴什博奕(Bash Game)
终于也轮到我做游戏了,他们做了好几个月的游戏了. 巴什博弈: 两个人做游戏,取石子,一个人最多可以可以取M个,至少取1个,最后取完的赢. 显然,如果n=m+1,那么由于一次最多只能取m个,所以,无论先 ...
- [bzoj5329] P4606 [SDOI2018]战略游戏
P4606 [SDOI2018]战略游戏:广义圆方树 其实会了圆方树就不难,达不到黑,最多算个紫 那个转换到圆方树上以后的处理方法,画画图就能看出来,所以做图论题一定要多画图,并把图画清楚点啊!! 但 ...
- 在IIS服务器上本地部署 ArcGIS API for js 4.15
作为一名刚入门的小白,还没开始一个helloworld就在软件安装,环境部署时遇到了一大堆问题,简直太让人头秃了,脑壳疼.话不多说,这篇主要想分享一下自己部署ArcGIS API for js 4.1 ...
- HBase Filter 过滤器之RowFilter详解
前言:本文详细介绍了HBase RowFilter过滤器Java&Shell API的使用,并贴出了相关示例代码以供参考.RowFilter 基于行键进行过滤,在工作中涉及到需要通过HBase ...
- 【原创】Linux Mutex机制分析
背景 Read the fucking source code! --By 鲁迅 A picture is worth a thousand words. --By 高尔基 说明: Kernel版本: ...