FBV装饰器:

def cook(request):

    err_msg=""
if request.method == "GET":
return render(request,'cookie.html') if request.method == "POST": username = request.POST.get('username')
p = request.POST.get('password') dic = user_info.get(username)
print(dic) if not dic:
err_msg="用户不存在"
return render(request,'cookie.html',{'err_msg':err_msg}) if dic['pwd'] == int(p): res = redirect('/xiaoqing/cookie1')
# res.set_cookie('username_cookie',username) #设置cookie 关闭浏览器失效
# res.set_cookie('username_cookie',username,max_age=10) 设置cookie失效时间 10秒过期
import datetime
current_date=datetime.datetime.utcnow()
change_date=current_date+datetime.timedelta(seconds=5)
res.set_cookie('username_cookie',username,expires=change_date) #到哪个时间失效 # res.set_signed_cookie('username_cookie',username,salt='sdasdas') return res
else:
return render(request,'cookie.html')

cook函数set cookie

def auth(func):  #装饰器 cookie认证

    def inner(request,*args,**kwargs):
v = request.COOKIES.get('username_cookie')
print(v)
if not v:
return redirect('/xiaoqing/cookie') return func(request,*args,**kwargs) return inner @auth
def cook1(request):
#获取当前已经登录的用户 v=request.COOKIES.get('username_cookie') #获取cookie
# 或者 v=request.COOKIES['username_cookie'] #获取cookie # v=request.get_signed_cookie('username_cookie',salt='sdasdas') #获取加密cookie return render(request,'cookie1.html',{'current_user':v,})

CBV装饰器

def auth(func): #装饰器

    def inner(request,*args,**kwargs):
v = request.COOKIES.get('username_cookie')
print(v)
if not v:
return redirect('/xiaoqing/cookie') return func(request,*args,**kwargs) return inner from django import views from django.utils.decorators import method_decorator #导入方法 @method_decorator(auth,name='dispatch') #类中全部方法做认证
class Order(views.View): # @method_decorator(auth) #单独方法做认证
def get(self,request):
v=request.COOKIES.get('username_cookie') return render(request,'cookie1.html',{'current_user':v,}) def post(self,request):
pass

FBV和CBV装饰器的更多相关文章

  1. django基础 -- 4. 模板语言 过滤器 模板继承 FBV 和CBV 装饰器 组件

    一.语法 两种特殊符号(语法): {{ }}和 {% %} 变量相关的用{{}},逻辑相关的用{%%}. 二.变量 1. 可直接用  {{ 变量名 }} (可调用字符串, 数字 ,列表,字典,对象等) ...

  2. CBV和FBV用户认证装饰器

    FBV装饰器用户验证 CBV装饰器用户验证 装饰器位置 或 或

  3. [Python自学] day-21 (2) (Cookie、FBV|CBV装饰器)

    一.什么是Cookie 1.什么是Cookie? Cookie是保存在客户端浏览器中的文件,其中记录了服务器让浏览器记录的一些键值对(类似字典). 当Cookie中存在数据时,浏览器在访问网站时会读取 ...

  4. django CBV装饰器 自定义django中间件 csrf跨站请求伪造 auth认证模块

    CBV加装饰器 第一种 @method_decorator(装饰器) 加在get上 第二种 @method_decorator(login_auth,name='get') 加在类上 第三种 @met ...

  5. Django基础七之CBV装饰器和中间件

    Django基础七之CBV装饰器和中间件 目录 Django基础七之CBV装饰器和中间件 1. CBV加装饰器 2. Django中间件 2.1 Django中间件介绍 2.2 自定义中间件 2.2. ...

  6. [oldboy-django][2深入django]FBV + CBV + 装饰器

    FBV django CBV & FBV - FBV function basic view a. urls 设置 urls(r'^test.html$', views.test) b. vi ...

  7. Django之Cookie Session详解,CBV,FBV登陆验证装饰器和自定义分页

    Cookie Session和自定义分页   cookie Cookie的由来 大家都知道HTTP协议是无状态的. 无状态的意思是每次请求都是独立的,它的执行情况和结果与前面的请求和之后的请求都无直接 ...

  8. cookie,session 的概念以及在django中的用法,以及cbv装饰器用法

    cookie的由来: 大家都知道HTTP协议是无状态的. 无状态的意思是每次请求都是独立的,它的执行情况和结果与前面的请求和之后的请求都无直接关系,它不会受前面的请求响应情况直接影响,也不会直接影响后 ...

  9. cbv装饰器 中间件 跨站请求伪造

    给cbv下面的函数加装饰器 写一个验证用户登录的程序 前端页面 # 写一个装饰器验证session def login_auth(func): def inner(request,*args,**kw ...

随机推荐

  1. HDU 1041 Computer Transformation(找规律加大数乘)

    主要还是找规律,然后大数相乘 #include<stdio.h> #include<string.h> #include<math.h> #include<t ...

  2. qtpy.PythonQtError: No Qt bindings could be found

     vnpy框架下No Qt bindings could be found  在 pycharm 里面出现  qtpy.PythonQtError: No Qt bindings could be f ...

  3. 三年磨一剑,robot framework 自动化测试框架核心指南,真正讲透robot framework自动化测试框架(笔者新书上架)。

    序 关于自动化测试的工具和框架其实有很多.自动化测试在测试IT行业中扮演着越来越重要的角色,不管是在传统的IT行业还是高速发展的互联网行业或是如今的大数据和大热的人工智能领域,都离不开测试,也更加离不 ...

  4. 《javascript经典入门》-day02

    <javascript经典入门>-day02 1.使用函数 1.1基本语法 function sayHello() { aler('Hello'); //...其他语句... } #关于函 ...

  5. CentOS7服务管理

    1.在/usr/lib/systemd/system目录下建立服务启动文件,文件格式:[root@Centos7 ]# cat /usr/lib/systemd/system/nginx.servic ...

  6. nginx的前世今生

    Nginx最初的设计是称为一个http服务器,一个能够解决C10K问题的http服务器. 那么问题来了,什么是C10K呢?C10K问题即(单机10万个并发链接问题),这个概念最早是由Dan Kegel ...

  7. Vue 组件异步加载(懒加载)

    一.vue的编译模式 (1)路由配置信息 //eg1: const MSite = resolve => require.ensure([], () =>resolve(require([ ...

  8. MVC架构模式详细说明

    一.简介: 架构模式是一个通用的.可重用的解决方案,用于在给定上下文中的软件体系结构中经常出现的问题.架构模式与软件设计模式类似,但具有更广泛的范围. 模型-视图-控制器模式,也称为MVC模式(Mod ...

  9. 在python3里面使用ueditor(基于adminx)

    第一步,下载ueditor,在git上下载 直接download : https://github.com/twz915/DjangoUeditor3/ 然后放到任何一个文件夹里面 配置setting ...

  10. 《网络是怎样连接的》PDF电子版书籍分享

    资料下载地址: 链接:https://pan.baidu.com/s/15tN9klTEsu-mQLayxI979g 提取码:ptu1 封面如下所示: