django的FBV和CBV的装饰器例子
备忘
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的装饰器例子的更多相关文章
- diango中的MTV——FBV/CBV以及装饰器的复用问题解决
MVC M: model 模型 与数据库交互 V: view 视图 HTML C:controller 控制器 流程 和 业务逻辑 MTV M:model ORM T:template 模板 HTML ...
- Django CBV加装饰器、Django中间件、auth模块
一. CBV加装饰器 在视图层中,基于函数的视图叫FBV(function base views),基于类的视图叫CBV(class base views).当需要用到装饰器时,例如之前的基于Cook ...
- django CBV 及其装饰器
#urls.py from django.contrib import admin from django.urls import path, re_path from app01 import vi ...
- CBV加装饰器解决登录注册问题和 <<中间件>>
文本目录 CBV加装饰器解决登录注册问题 一:什么是中间件 二:中间件有什么用 三:自定义中间件 四:中间件应用场景 五:SCRF TOKEN跨站请求伪造 六: 其他操作 CBV加装饰器解决登录注册问 ...
- Django视图函数函数之视图装饰器
FBV模式装饰器: 普通函数的装饰器(语法糖@) views.py from django.shortcuts import render def wrapper(f): def inner(*arg ...
- Cookie与Session、CBV添加装饰器
cookie Cookie的由来 大家都知道HTTP协议是无状态的. 无状态的意思是每次请求都是独立的,它的执行情况和结果与前面的请求和之后的请求都无直接关系,它不会受前面的请求响应情况直接影响,也不 ...
- 浅谈Django的中间件与Python的装饰器
浅谈Django的中间件 与Python的装饰器 一.原理 1.装饰器是Python的一种语法应用,利用闭包的原理去更改一个函数的功能,即让一个函数执行之前先到另外一个函数中执行其他需求语句,在执行该 ...
- Django视图函数:CBV与FBV (ps:补充装饰器)
CBV 基于类的视图 FBV 基于函数的视图 CBV: 1 项目目录下: 2 urlpatterns = [ 3 path('login1/',views.Login.as_view()) #.as ...
- 基于Django-Cookie的CBV和FBV的用户验证装饰器
FBV模式 def cookie(func): def deco(request,*args,**kwargs): u = request.get_signed_c ...
随机推荐
- Python实现Plugin(插件化开发)
https://www.cnblogs.com/terencezhou/p/10276167.html
- c#万能视频播放器
http://blog.csdn.net/yanzhibo/article/details/8972822 本人之前很多的文章中均提到了使用libvlc为播放器内核制作的播放器,也许有些朋友对此感兴趣 ...
- Django多表查询练习题
#一 model表:from django.db import models # Create your models here. class Teacher(models.Model): tid=m ...
- LOJ6072苹果树
虽然结合了很多算法,但是一步一步地推一下还不算太难的一道题. 首先考虑枚举枚举有用的苹果的集合,然后去算生成树个数. 先考虑怎么计算生成树个数. 发现可以使用matrix-tree. 所有有用点可以和 ...
- mysql存储引擎的对比
- C# string 字符串详解 恒定 驻留
string是一种很特殊的数据类型,它既是基元类型又是引用类型,在编译以及运行时,.Net都对它做了一些优化工作,正式这些优化工作有时会迷惑编程人员,使string看起来难以琢磨.这篇文章共四节,来讲 ...
- 把javabean复制到另一个javabean 使用BeanUtils.copyProperties(a,b) 复制
该方法对于两种不同的jar包有两种不同的意义 ,a,b通常是两个结构相似的javabean,注意:a,b里的定义类型名称必须一致才能复制 引用的是org.springframework.beans 则 ...
- Leetcode 117
if(root == NULL) return; queue<TreeLinkNode *> que; que.push(root); while(!empty(que)){ int le ...
- (效率低下)77. Combinations C++回溯法 组合
https://leetcode.com/problems/combinations/ 沿用78题的思路 class Solution { public: void backTrack(vector& ...
- lua 函数基础
函数定义在前,调用在后 如果函数只有一个实参,并且此参数是一个字面字符串或者table构造式,那么可以省略() 例如 print "hello" unpack{1,2} print ...