添加登录装饰器的两种方式:FBV和CBV
1、FBV方式:添加验证装饰器
def auth(func): def deco(request, *args, **kwargs): u = request.get_signed_cookie('username', salt='user', default=None) if not u: return render(request, 'login.html') return func(request, *args, **kwargs) return deco
@authdef index(request): u = request.get_signed_cookie('username', salt='user', default=None) return render(request, 'index.html', {'user': u})
@authdef detail(request): u = request.get_signed_cookie('username', salt='user', default=None) return render(request, 'detail.html', {'user': u})
访问index/detail时,调用auth装饰器,如果验证成功,则执行index/detail(return func(request, *args, **kwargs)语句起的作用);否则跳转到login.html
2、CBV方式:通过django自带的装饰器method_decorator 的@method_decorator(cookie)来实现
from django.utils.decorators import method_decoratorfrom django import views
# @method_decorator(cookie,name='dispatch') # dispatch的便捷写法class CBVtest(views.View): @method_decorator(cookie) # 给dispatch方法添加装饰器,那么下面所有的get,post都会添加 def dispatch(self, request, *args, **kwargs): return super(CBVtest, self).dispatch(request, *args, **kwargs)
# @method_decorator(cookie) # 单独添加 def get(self, request): u = request.get_signed_cookie('username', salt='user', default=None) return render(request, 'houtai.html', {'user': u})
def post(self, request): return HttpResponse('post ok')
添加登录装饰器的两种方式:FBV和CBV的更多相关文章
- Mac Item2 SSH免密登录Linux 服务器的两种方式
转自http://blog.csdn.net/jobschen/article/details/52823980 mac ssh登录linux服务器 的两种方式: 个人推荐第二种,zsh方式,只需要把 ...
- cmd窗口使用sftp命令非密钥和密钥登录SFTP服务器的两种方式
cmd窗口使用sftp命令非密钥和密钥登录SFTP服务器的两种方式 一.在Windows环境下搭建SFTP服务器可参见http://www.cnblogs.com/Kevin00/p/6341295. ...
- Django(十六)基于模板的登录案例:登录装饰器、csrf攻击方式及防护、ajax的Post 的csrf开启写法、生成验证码、加验证码登录、反向解析+传参
一.csrf攻击 1.1 csrf攻击(跨站请求伪造) [csrf攻击即]:通过第3方网站,伪造请求(前提条件是你已经登录正常网站,并保存了session或cookie登录信息且没有退出),第三方网站 ...
- python之装饰器的两种写法
上一篇文章介绍了 装饰器的概念.现在讲一下在程序中怎么来写装饰器.上代码: def X(fun): def Y(b): print(b) fun() return Y def test(): prin ...
- servlet之session添加和移除的两种方式
Java Session 介绍 一.添加.获取session 1.项目结构 2.jar包 3.web.xml文件 <?xml version="1.0" encoding=& ...
- python缓存装饰器,第二种方式(二)
来个简单的装饰器 def cached_method_result(fun): """方法的结果缓存装饰器""" @wraps(fun) d ...
- python locust_TaskSet声明任务的典型方法是使用task装饰器的两种方法
为TaskSet声明任务的典型方法是使用task装饰器.该min_wait和MAX_WAIT属性也可以在使用taskset类中重写. from locust import Locust, TaskSe ...
- Java使用拦截器的两种方式
拦截器是个好东西,之前用到过,现在记录一下,供以后参考使用! 其一,使用org.aspectj.lang.annotation.Aspect 先上代码: package com.test.interc ...
- django class类即视图类添加装饰器的几种方法
根据别人发布整理,个人爱好收集(原文:https://blog.csdn.net/mydistance/article/details/83958655 ) 第一种:定义函数装饰器,在函数,类中使用函 ...
随机推荐
- argparse 使用指南
argparse是Python标准库中推荐使用的命令行解析模块, 其前身是optparse库,从Python 2.7开始,optparse库被弃用, 替代它的就是argparse库,除此之外,标准库中 ...
- Struts2监听Action结果的监听器
作者:禅楼望月 在前面我们学到了在特定的Action中配置结果监听器,在Action完成控制处理之后,struts2转入实际的物理视图之前被回调.但是这种方式的缺点是,结果的监听器不能被复用.根据设计 ...
- Visio中的Undo和Redo
1.Visio默认Undo和Redo操作是可用的,Appliacation中的UndoEnabled标志Undo和Redo操作是否可用. m_Visio.Window.Application.Undo ...
- [Leetcode] Linked list cycle 判断链表是否有环
Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...
- 如何写出规范的JavaScript代码
作为一名开发人员(WEB前端JavaScript开发),不规范的开发不仅使日后代码维护变的困难,同时也不利于团队的合作,通常还会带来代码安全以及执行效率上的问题.本人在开发工作中就曾与不按规范来开发的 ...
- 从零开始学习MXnet(一)
最近工作要开始用到MXnet,然而MXnet的文档写的实在是.....所以在这记录点东西,方便自己,也方便大家. 我觉得搞清楚一个框架怎么使用,第一步就是用它来训练自己的数据,这是个很关键的一步. 一 ...
- [poj 3281]最大流+建图很巧妙
题目链接:http://poj.org/problem?id=3281 看了kuangbin大佬的思路,还用着kuangbin板子orz http://www.cnblogs.com/kuangb ...
- bzoj 2566 calc 拉格朗日插值
calc Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 377 Solved: 226[Submit][Status][Discuss] Descr ...
- 图论:费用流-SPFA+EK
利用SPFA+EK算法解决费用流问题 例题不够裸,但是还是很有说服力的,这里以Codevs1227的方格取数2为例子来介绍费用流问题 这个题难点在建图上,我感觉以后还要把网络流建模想明白才能下手去做这 ...
- centos7.6升级ssh7.9、安装PHP7.2、Nginx1.15.9、PHP加密扩展php_screw1.5
1.centos7 安装PHP7.2版本 #查询是否安装过php yum list installed | grep php yum provides php #移除php yum remove ph ...