备忘

def auth(func):
def inner(request,*args,**kwargs):
u = request.COOKIES.get('username111')
if not u:
return redirect('/login/')
print("call index()")
return func(request,*args,**kwargs)
return inner #FBV
@auth
def index(request): #带签名的cookie
obj = HttpResponse('')
obj.set_signed_cookie('username','',salt='adasaa')#salt加盐,设置cookie,加密
request.get_signed_cookie('username','',salt='adasaa')#获取cookie u = request.COOKIES.get('username111')
if not u:
return redirect('/login/')
else:
return render(request,'index.html',{'u':u}) #CBV
from django.views import View
from django.utils.decorators import method_decorator
class Order(View): @method_decorator(auth)
def get(self,request):
u = request.COOKIES.get('username111')
return render(request, 'index.html', {'u': u}) def post(self,request):
u = request.COOKIES.get('username111')
if not u:
return redirect('/login/')
else:
return render(request, 'index.html', {'u': u})

CBV如果有很多方法,都要加装饰器的话,可以加在dispatch方法上,这样类里面的方法就不用再一一加上装饰了

def auth(func):
def inner(request,*args,**kwargs):
u = request.COOKIES.get('username111')
if not u:
print("未登陆")
return redirect('/login/')
print("call now")
return func(request,*args,**kwargs)
return inner #FBV
@auth
def index(request): #带签名的cookie
obj = HttpResponse('')
obj.set_signed_cookie('username','',salt='adasaa')#salt加盐,设置cookie,加密
request.get_signed_cookie('username','',salt='adasaa')#获取cookie u = request.COOKIES.get('username111')
if not u:
return redirect('/login/')
else:
return render(request,'index.html',{'u':u}) #CBV
from django.views import View
from django.utils.decorators import method_decorator
class Order(View): @method_decorator(auth)
def dispatch(self, request, *args, **kwargs):
return super(self,Order).dispatch(request, *args, **kwargs) def get(self,request):
u = request.COOKIES.get('username111')
return render(request, 'index.html', {'u': u}) def post(self,request):
u = request.COOKIES.get('username111')
if not u:
return redirect('/login/')
else:
return render(request, 'index.html', {'u': u})

再来一个终极版的,不用重写dispatch,直接加在类上面:

def auth(func):
def inner(request,*args,**kwargs):
u = request.COOKIES.get('username111')
if not u:
print("未登陆")
return redirect('/login/')
print("call now")
return func(request,*args,**kwargs)
return inner #FBV
@auth
def index(request): #带签名的cookie
obj = HttpResponse('')
obj.set_signed_cookie('username','',salt='adasaa')#salt加盐,设置cookie,加密
request.get_signed_cookie('username','',salt='adasaa')#获取cookie u = request.COOKIES.get('username111')
if not u:
return redirect('/login/')
else:
return render(request,'index.html',{'u':u}) #CBV
from django.views import View
from django.utils.decorators import method_decorator @method_decorator(auth,name='dispatch')
class Order(View): # @method_decorator(auth)
# def dispatch(self, request, *args, **kwargs):
# return super(self,Order).dispatch(request, *args, **kwargs) def get(self,request):
u = request.COOKIES.get('username111')
return render(request, 'index.html', {'u': u}) def post(self,request):
u = request.COOKIES.get('username111')
if not u:
return redirect('/login/')
else:
return render(request, 'index.html', {'u': u})

django的FBV和CBV的装饰器例子的更多相关文章

  1. diango中的MTV——FBV/CBV以及装饰器的复用问题解决

    MVC M: model 模型 与数据库交互 V: view 视图 HTML C:controller 控制器 流程 和 业务逻辑 MTV M:model ORM T:template 模板 HTML ...

  2. Django CBV加装饰器、Django中间件、auth模块

    一. CBV加装饰器 在视图层中,基于函数的视图叫FBV(function base views),基于类的视图叫CBV(class base views).当需要用到装饰器时,例如之前的基于Cook ...

  3. django CBV 及其装饰器

    #urls.py from django.contrib import admin from django.urls import path, re_path from app01 import vi ...

  4. CBV加装饰器解决登录注册问题和 <<中间件>>

    文本目录 CBV加装饰器解决登录注册问题 一:什么是中间件 二:中间件有什么用 三:自定义中间件 四:中间件应用场景 五:SCRF TOKEN跨站请求伪造 六: 其他操作 CBV加装饰器解决登录注册问 ...

  5. Django视图函数函数之视图装饰器

    FBV模式装饰器: 普通函数的装饰器(语法糖@) views.py from django.shortcuts import render def wrapper(f): def inner(*arg ...

  6. Cookie与Session、CBV添加装饰器

    cookie Cookie的由来 大家都知道HTTP协议是无状态的. 无状态的意思是每次请求都是独立的,它的执行情况和结果与前面的请求和之后的请求都无直接关系,它不会受前面的请求响应情况直接影响,也不 ...

  7. 浅谈Django的中间件与Python的装饰器

    浅谈Django的中间件 与Python的装饰器 一.原理 1.装饰器是Python的一种语法应用,利用闭包的原理去更改一个函数的功能,即让一个函数执行之前先到另外一个函数中执行其他需求语句,在执行该 ...

  8. Django视图函数:CBV与FBV (ps:补充装饰器)

    CBV 基于类的视图  FBV 基于函数的视图 CBV: 1 项目目录下: 2 urlpatterns = [ 3 path('login1/',views.Login.as_view()) #.as ...

  9. 基于Django-Cookie的CBV和FBV的用户验证装饰器

    FBV模式 def cookie(func):       def deco(request,*args,**kwargs):             u = request.get_signed_c ...

随机推荐

  1. English trip M1 - AC11 May I Help You? 我能帮到你吗? Teacher:Lamb

    In this lesson you will learn to ask for things in shops  在本课程中,您将学习如何在商店中寻找东西 课上内容(Lesson) How are ...

  2. codeforces708b// Recover the String //AIM Tech Round 3 (Div. 1)

    题意:有一个01组成的串,告知所有长度为2的子序列中,即00,01,10,11,的个数a,b,c,d.输出一种可能的串. 先求串中0,1的数目x,y. 首先,如果00的个数a不是0的话,设串中有x个0 ...

  3. POJ-2955 Brackets(括号匹配问题)

    题目链接:http://poj.org/problem?id=2955 这题要求求出一段括号序列的最大括号匹配数量 规则如下: the empty sequence is a regular brac ...

  4. 矩阵最优路线DP

    母题:矩阵中每个点有权值,每经过一个点就累加权值,求从a点到b点的最优(最大)路线. 题型1: 从左上到右下,只能向下或者向右 for 行 for 列 dp=max dp左,dp上; 扫一遍就行 有时 ...

  5. Vue音乐项目笔记(一)

    看到一位小可爱的手记,这里记录一下自己需要注意的地方的链接 1.手写轮播图(上) https://blog.csdn.net/weixin_40814356/article/details/80298 ...

  6. 在项目中使用react

    1. 运行 ’cnpm i react react-dom -S' 安装包 react:专门用于创建组件和虚拟DOM,同时组件的生命周期都在这个包中 react-dom:专门进行DOM操作,主要应用场 ...

  7. 【洛谷p1319】压缩技术

    (许久不见,甚是想念) 压缩技术[传送门] 洛谷上滴算法标签: 然而这是一道入门难度的题.(不管不管,就写它了) 好的先说一下思路吧wait!我忘记了咋做的当时. 首先做题第一道坎儿,如何输入若干个( ...

  8. 『计算机视觉』Mask-RCNN

    一.Mask-RCNN流程 Mask R-CNN是一个实例分割(Instance segmentation)算法,通过增加不同的分支,可以完成目标分类.目标检测.语义分割.实例分割.人体姿势识别等多种 ...

  9. spring boot(十二)打包部署

    有很多网友会时不时的问我,spring boot项目如何测试,如何部署,在生产中有什么好的部署方案吗?这篇文章就来介绍一下spring boot 如何开发.调试.打包到最后的投产上线. 开发阶段 单元 ...

  10. verilog的移位运算符(存在不公平现象)

    从上面的例子可以看出,start在移过两位以后,用0来填补空出的位.进行移位运算时应注意移位前后变量的位数,下面举例说明. 4’b1001<<1 = 5’b10010; //左移1位后用0 ...