Django基础之给视图加装饰器
1. 使用装饰器装饰FBV
FBV本身就是一个函数,所以和给普通的函数加装饰器无差:
```Python
def wrapper(func):
def inner(*args, **kwargs):
start_time = time.time()
ret = func(*args, **kwargs)
end_time = time.time()
print("used:", end_time-start_time)
return ret
return inner
@wrapper
def add_class(request):
if request.method == "POST":
class_name = request.POST.get("class_name")
models.Classes.objects.create(name=class_name)
return redirect("/classa/")
return render(request, "add_class.html")
<h2>2. 使用装饰器装饰CBV</h2>
类中的方法与独立函数不完全相同,因此不能直接将函数装饰器应用于类中的方法,我们需要先将其转换为方法装饰器。<br>
django中提供了method_decorator装饰器用于将函数装饰器转化为方法装饰器。<br>
使用method_decorator有2种方法能装饰CBV。
<h3>2.1 给dispatch方法加装饰器</h3>
```Python
from django.vies import View
from django.utils.decorators import method_decorator
from django.shortcuts import render, HttpResponse, redirect
def wrapper(func):
def inner(*args, **kwargs):
start_time = time.time()
ret = func(*args, **kwargs)
end_time = time.time()
print("used:", end_time-start_time)
return ret
return inner
class Login(View):
@method_decorator(wrapper)
def dispatch(self, request, *args, **kwargs):
obj = super(Login, self).dispatch(request, *args, **kwargs)
return obj
def get(self, request):
return render(request, "login.html")
def post(self, request):
print(request.POST.get("user"))
return HttpResponse("Login.post")
2.3 在类上添加装饰器(推荐)
```Python
from django.views import View
from django.utils.decorators import method_decorator
from django.shortcuts import render, HttpResponse, redirect
def wrapper(func):
def inner(args, **kwargs):
start_time = time.time()
ret = func(args, **kwargs)
end_time = time.time()
print("used:", end_time-start_time)
return ret
return inner
@method_decorator(wrapper, name="dispatch")
class AddClass(View):
def get(self, request):
return render(request, "add_class.html")
def post(self, request):
class_name = request.POST.get("class_name")
models.Classes.objects.create(name=class_name)
return redirect("/class/")
Django基础之给视图加装饰器的更多相关文章
- Django---CBV和FBV的使用,CBV的流程,给视图加装饰器,Request对象方法,属性和Response对象,form表单的上传
Django---CBV和FBV的使用,CBV的流程,给视图加装饰器,Request请求对象方法,属性和Response响应对象,form表单的上传 一丶CBV和FBV 在Django中存 ...
- Django-给视图加装饰器
给FBV加装饰器 FBV:function based view FBV本身就是一个函数,所以跟普通函数加装饰器是一样的 # 装饰函数是要在APP文件中定义,本例是在app01\templatetag ...
- Django基础三之视图函数
一 Django的视图函数view 一个视图函数(类),简称视图,是一个简单的Python 函数(类),它接受Web请求并且返回Web响应. 响应可以是一张网页的HTML内容,一个重定向,一个404错 ...
- 03.Django基础三之视图函数
一 Django的视图函数view 一个视图函数(类),简称视图,是一个简单的Python 函数(类),它接受Web请求并且返回Web响应. 响应可以是一张网页的HTML内容,一个重定向,一个404错 ...
- day 53-1 Django基础三之视图函数
Django基础三之视图函数 本节目录 一 Django的视图函数view 二 CBV和FBV 三 使用Mixin 四 给视图加装饰器 五 Request对象 六 Response对象 一 Dja ...
- day 67 Django基础三之视图函数
Django基础三之视图函数 本节目录 一 Django的视图函数view 二 CBV和FBV 三 使用Mixin 四 给视图加装饰器 五 Request对象 六 Response对象 一 Dja ...
- Django CBV加装饰器、Django中间件、auth模块
一. CBV加装饰器 在视图层中,基于函数的视图叫FBV(function base views),基于类的视图叫CBV(class base views).当需要用到装饰器时,例如之前的基于Cook ...
- django ----CBV中加装饰器
CBV中加装饰器 from django import views from django.utils.decorators import method_decorator def login_aut ...
- Django中类视图使用装饰器的方式
类视图使用装饰器 为类视图添加装饰器,可以使用两种方法. 为了理解方便,我们先来定义一个为函数视图准备的装饰器(在设计装饰器时基本都以函数视图作为考虑的被装饰对象),及一个要被装饰的类视图. def ...
随机推荐
- 数据库优化方案之SQL脚本优化
随着数据库数据越来越大,数据单表存在的数据量也就随之上去了,那么怎么样让我们的脚本查询数据更快呢? 在这个地方我们主要提到两个数据库类型: 1.MSSQL(该数据库我们通过执行计划来查看数据库性能在哪 ...
- tinymce富文本是在modal框中弹出显示的问题
记录一下,在用tinymce富文本的时候,由于是用在modal 上的,始终无法获取焦点,后来才发现问题出在tinymce在modal前创建了,所以导致这个问题,解决方案就是用 v-if="v ...
- 【原创】大叔经验分享(77)openresty(nginx+lua)发http请求
openresty(nginx+lua)发http请求 利用location+proxy_pass间接实现 location ^~ /test/http { internal; proxy_pass ...
- 6.单表的CRUD操作
1.插入后用新id初始化被插入对象 <insert id="insertStudentCatchId"> insert into student (age,name,s ...
- Java动态追踪技术探究(动态修改)
Java动态追踪技术探究 Java探针-Java Agent技术-阿里面试题 秒懂Java动态编程(Javassist研究) 可以用于在类加载的时候,修改字节码. Java agent(Java探针) ...
- .net 调用 winapi获取窗口句柄和内容
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 玩转springcloud(三):服务的提供者与调用者(注册于发现)
一.简介 上文我们实践了cloud的注册中心的单服务于多节点的搭建,房子造好了得有人来住不是,这篇我们实践下服务提供者于调用者的案例,也就是服务端和客户端的调用. 本文会设计三个module:注册中心 ...
- (一)Android jni打印到logcat
#include <stdio.h> #include <android/log.h> int main(void) { int a = 0x10,b = 0x20; __an ...
- 关于JPype报FileNotFoundError: [Errno 2] No such file or directory: '/usr/lib/jvm'错误的解决
部署到线上的项目正常运行一年,今天早上突然报FileNotFoundError: [Errno 2] No such file or directory: '/usr/lib/jvm'错误. JPyp ...
- 剖析ajax
学过javascript和接触过后端PHP语言必然要用到ajax,这是必学的一门学科,AJAX指的是Asynchronous JavaScript and XML,它使用XMLHttpRequest对 ...