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 ...
随机推荐
- English trip M1 - AC9 Nosey people 爱管闲事的人 Teacher:Solo
In this lesson you will learn to talk about what happened. 在本课中,您将学习如何谈论发生的事情. 课上内容(Lesson) # four “ ...
- 20180518VSTO多簿单表汇总外接程序按钮
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsof ...
- ActionCable的部署(参考Gorails)
Gorails视频 https://gorails.com/deploy/actioncable Action Cable – Integrated WebSockets for Rails http ...
- Elsevier系旗下期刊论文投稿流程
目录 1.上传文件需求 2.注册账号和填写相关信息 3.以作者身份登入 4.开始提交论文:点击submit New Manuscript 5.选择论文类型:一般是科技长文Full Length Art ...
- LitePal 数据库使用方法(最新2.0LitePal数据库适用)
转发郭神的blog,讲的非常详细,是基于1.6版本,但现在使用的是2.0,有点差别https://blog.csdn.net/guolin_blog/article/details/384612391 ...
- php-fpm.conf文件的位置在哪里
在php的安装目录下的etc目录下:
- 四则运算web最终版
经过若干时间的奋战,终于完成了web版四则运算程序.团队成员:井小普.张贺. 设计思想: 在之前的程序基础上两人结合开发web系统. 首先,进行登录注册界面的编写,不同用户,对应不同的错题库,答题记录 ...
- Leetcode 969. 煎饼排序
969. 煎饼排序 显示英文描述 我的提交返回竞赛 用户通过次数134 用户尝试次数158 通过次数135 提交次数256 题目难度Medium 给定数组 A,我们可以对其进行煎饼翻转:我们选择 ...
- noip2014无线网络发射器选址
题目描述 随着智能手机的日益普及,人们对无线网的需求日益增大.某城市决定对城市内的公共场所覆盖无线网. 假设该城市的布局为由严格平行的 129 条东西向街道和 129 条南北向街道所形成的网格状,并且 ...
- iOS 面试总结
APP崩溃 启动秒退 在新 iOS 上正常的应用,到了老版本 iOS 上秒退最常见原因是系统动态链接库或Framework无法找到.这种情况通常是由于 App 引用了一个新版操作系统里的动态库(或者某 ...