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的更多相关文章

  1. Mac Item2 SSH免密登录Linux 服务器的两种方式

    转自http://blog.csdn.net/jobschen/article/details/52823980 mac ssh登录linux服务器 的两种方式: 个人推荐第二种,zsh方式,只需要把 ...

  2. cmd窗口使用sftp命令非密钥和密钥登录SFTP服务器的两种方式

    cmd窗口使用sftp命令非密钥和密钥登录SFTP服务器的两种方式 一.在Windows环境下搭建SFTP服务器可参见http://www.cnblogs.com/Kevin00/p/6341295. ...

  3. Django(十六)基于模板的登录案例:登录装饰器、csrf攻击方式及防护、ajax的Post 的csrf开启写法、生成验证码、加验证码登录、反向解析+传参

    一.csrf攻击 1.1 csrf攻击(跨站请求伪造) [csrf攻击即]:通过第3方网站,伪造请求(前提条件是你已经登录正常网站,并保存了session或cookie登录信息且没有退出),第三方网站 ...

  4. python之装饰器的两种写法

    上一篇文章介绍了 装饰器的概念.现在讲一下在程序中怎么来写装饰器.上代码: def X(fun): def Y(b): print(b) fun() return Y def test(): prin ...

  5. servlet之session添加和移除的两种方式

    Java Session 介绍 一.添加.获取session 1.项目结构 2.jar包 3.web.xml文件 <?xml version="1.0" encoding=& ...

  6. python缓存装饰器,第二种方式(二)

    来个简单的装饰器 def cached_method_result(fun): """方法的结果缓存装饰器""" @wraps(fun) d ...

  7. python locust_TaskSet声明任务的典型方法是使用task装饰器的两种方法

    为TaskSet声明任务的典型方法是使用task装饰器.该min_wait和MAX_WAIT属性也可以在使用taskset类中重写. from locust import Locust, TaskSe ...

  8. Java使用拦截器的两种方式

    拦截器是个好东西,之前用到过,现在记录一下,供以后参考使用! 其一,使用org.aspectj.lang.annotation.Aspect 先上代码: package com.test.interc ...

  9. django class类即视图类添加装饰器的几种方法

    根据别人发布整理,个人爱好收集(原文:https://blog.csdn.net/mydistance/article/details/83958655 ) 第一种:定义函数装饰器,在函数,类中使用函 ...

随机推荐

  1. javasisst & JAVA8

    今天在服务器上启动tomcat7的时候,提示如下异常: java.io.IOException: invalid constant type: 15 具体看是javasisst抛出来的. 系统运行环境 ...

  2. Shiro 的 HelloWorld

    密码文件 [users] zhang=123 wang=123 测试 package org.zln.hello; import org.apache.log4j.LogManager; import ...

  3. [luogu P1442] 铁球落地

    题目链接 线段树\(+dp\). 先用线段树预处理出每个线段从左边和右边掉落到哪里,记为\(f[i][0/1]\). 然后记\(g[i][0/1]\)为到达第\(i\)个线段的左边或右边所要的最小时间 ...

  4. [Leetcode] Same tree判断是否为相同树

    Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...

  5. jsp电子商务购物车之四 数据库存储篇

    为了方便用户下次登录,仍然可以看到自己的购物车内容,所以,需要在数据库存储相应的购物车项目,本处增加购物车项表;uid和bid是复合主键. package com.cart.entity; //购物车 ...

  6. CentOS 7, 升级python到3.x

    By francis_hao    Apr 11,2017 使用源码安装方式 首先到官网https://www.python.org/downloads/source/ 下载python最新版本.当前 ...

  7. Watto and Mechanism Codeforces Round #291 (Div. 2)

    C. Watto and Mechanism time limit per test 3 seconds memory limit per test 256 megabytes input stand ...

  8. xmlns:sys="clr-namespace:System;assembly=mscorlib" NOTE: System;与assembly中间不能有空格

    xmlns:sys="clr-namespace:System;assembly=mscorlib"  NOTE: System;与assembly中间不能有空格 否则报错, Er ...

  9. JS模块化工具requirejs教程02

    基本API require会定义三个变量:define,require,requirejs,其中require === requirejs,一般使用require更简短 define 从名字就可以看出 ...

  10. Chrome 本地通信

    http://blog.csdn.net/ztmaster/article/details/52684772