CBV 验证装饰器的使用
下面是3种方式:
from django.shortcuts import render, redirect
from django.views import View
# Create your views here.class Login(View):def get(self, request):return render(request, 'login.html')def check_login(func):def inner(request, *args, **kwargs):if request.session.get('user_info'):return func(request, *args, **kwargs)else:return redirect('/login.html')return innerfrom django.utils.decorators import method_decorator@method_decorator(check_login, name='dispatch')
class Index(View):@method_decorator(check_login)def dispatch(self, request, *args, **kwargs):return super(Index,self).dispatch( request, *args, **kwargs)@method_decorator(check_login)def get(self, request):return render(request, 'index.html')def post(self, request):return render(request, 'index.html')
转载于:https://www.cnblogs.com/wumingxiaoyao/p/6529963.html
CBV 验证装饰器的使用相关推荐
- csrf跨站请求伪造,CBV添加装饰器,auth认证模块,基于django中间件设计项目功能
文章目录 csrf跨站请求伪造 csrf的定义 csrf的分类 csrf的攻击过程 csrf的攻击条件 举例说明 Django提供的解决策略 csrf相关装饰器 FBV CBV 方法一(直接在类中的某 ...
- django14:CBV加入装饰器
加在方法上面 from django.utils.decorators import method_decoratorclass HomeView(View):def dispatch(self, r ...
- django CBV装饰器 自定义django中间件 csrf跨站请求伪造 auth认证模块
CBV加装饰器 第一种 @method_decorator(装饰器) 加在get上 第二种 @method_decorator(login_auth,name='get') 加在类上 第三种 @met ...
- Django cookie、session与CBV装饰器
文章目录 Django cookie.session与CBV装饰器 cookie与session 什么是Cookie 查看Cookie Django中操作Cookie 什么是Session Djang ...
- python 自定义装饰器 来验证函数参数
# -*- coding: utf-8 -*- # by dl from inspect import signature # 用来获取函数的一些信息# 自定义参数验证装饰器 def typeasse ...
- Python小知识点(3)--装饰器
Python小知识点(3)--装饰器 (1)装饰器含参数,被装饰函数不含(含)参数 实例代码如下: import time # 装饰器函数 def wrapper(func):def done(*ar ...
- python3 装饰器_Python3 装饰器
一 什么是装饰器呢? 装饰器本质上来说就是函数,功能就是为其它函数添加附加功能. 原则: 不修改被修饰函数的源代码 不修改被修饰函数的调用方式 组织结构: 装饰器=高阶函数+函数嵌套+闭包 实例1(P ...
- 装饰器,生成器,迭代器
文章目录 装饰器 什么是装饰器 为什么使用装饰器 装饰器的使用场景 用户认证,判断用户是否登录 计算函数运行时间(算是一个功能,在项目中用的不多) 记录日志 redis缓存 装饰器1----能够适应9 ...
- 线程同步 线程安全_同步装饰器来替换线程安全类
线程同步 线程安全 您知道什么是线程安全吗? 如果没有,下面是一个简单的示例. 所有类都必须是线程安全的,对吗? 并不是的. 其中一些必须是线程安全的? 又错了. 我认为它们都不必是线程安全的,而它们 ...
最新文章
- CKEditor+CKFinder配置学习
- unicode 字符集环境下的mfc 读写 ini 配置文件的_WSL:在Windows下优雅地玩Linux
- 数据结构——树的简单操作集合
- 理解分布式一致性:Paxos协议之Cheap Paxos Fast Paxos
- 发现几个常用的asp.net MVC Helper 源码
- 好象现在才开始热爱生活了
- 服务中没有telnet_win7 服务中没有 prints pooler
- Math Type World2016安装
- 关于Jquery.Data()和HTML标签的data-*属性
- Codeforces Round #524 (Div. 2) Masha and two friends
- swing禁用最大化、去掉java图标、设置窗口为中央显示
- 简简单单做股票读书笔记(1/8)
- 计算机主板启动显示器黑屏,电脑开机显示器黑屏怎么回事_开机显示器黑屏怎么解决...
- 基于android studio真机连接本地服务器(Apache)详细流程
- 正则表达式 企业统一社会信用代码、企业13位工商注册号、企业组织机构代码...
- iOS 项目源码大全 github 国内外大神
- 牛客网数据库SQL实战25—— 获取员工其当前的薪水比其manager当前薪水还高的相关信息
- php route,FastRoute
- Gruobi多目标规划
- 人工智能基础(高中版)教材补充和资源分享