FBV + CBV

django中请求处理方式有2种:FBV 和 CBV

FBV(function bases views)

就是在视图里使用函数处理请求,如下:

# urls.py

from django.conf.urls import url, include
from app01 import views urlpatterns = [
url(r'^index/', views.index),
]
# views.py

from django.shortcuts import render

def index(req):
if req.method == 'POST':
print('method is :' + req.method)
elif req.method == 'GET':
print('method is :' + req.method)
return render(req, 'index.html')

注意此处定义的是函数【def index(req):】

<!--index.html-->

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>index</title>
</head>
<body>
<form action="" method="post">
<input type="text" name="A" />
<input type="submit" name="b" value="提交" />
</form>
</body>
</html>

FBV中加装饰器相关

def deco(func):
def wapper(request,*agrs,**kwargs):
if request.COOKIES.get('LOGIN'):
return func(request, *args, **kwargs)
return redirect('/login/')
return wrapper @deco
def index(req):
if req.method == 'POST':
print('method is :' + req.method)
elif req.method == 'GET':
print('method is :' + req.method)
return render(req, 'index.html')

上面就是FBV的使用。

CBV(class bases views)

就是在视图里使用类处理请求,如下:

# urls.py

from app01 import views

urlpatterns = [
url(r'^index/', views.Index.as_view()),
]
# views.py

from django.views import View

# 类要继承View ,类中函数名必须小写
class Index(View):
def get(self, req):
'''
处理GET请求
'''
print('method is :' + req.method)
return render(req, 'index.html') def post(self, req):
'''
处理POST请求
'''
print('method is :' + req.method)
return render(req, 'index.html')

CBV中加装饰器相关

要在CBV视图中使用我们上面的check_login装饰器,有以下三种方式:

  1. 加在CBV视图的get或post方法上

    from django.utils.decorators import method_decorator
    from django.views import View class Index(View): def dispatch(self,req,*args,**kwargs):
    return super(Index,self).dispatch(req,*args,**kwargs) def get(self, req):
    print('method is :' + req.method)
    return render(req, 'index.html') @method_decorator(deco)
    def post(self, req):
    print('method is :' + req.method)
    return render(req, 'index.html')
  2. 加在diapatch方法上,因为CBV中首先执行的就是dispatch方法,所以这么写相当于给get和post方法都加上了登录校验。

    from django.utils.decorators import method_decorator
    from django.views import View class Index(View): @method_decorator(deco)
    def dispatch(self,req,*args,**kwargs):
    return super(Index,self).dispatch(req,*args,**kwargs) def get(self, req):
    print('method is :' + req.method)
    return render(req, 'index.html') def post(self, req):
    print('method is :' + req.method)
    return render(req, 'index.html')
  3. 直接加在视图类上,但method_decorator必须传name关键字参数

    如果get方法和post方法都需要登录校验的话就写两个装饰器

    from django.utils.decorators import method_decorator
    from django.views import View @method_decorator(deco,name='get')
    @method_decorator(deco,name='post')
    class Index(View): def dispatch(self,req,*args,**kwargs):
    return super(Index,self).dispatch(req,*args,**kwargs) def get(self, req):
    print('method is :' + req.method)
    return render(req, 'index.html') def post(self, req):
    print('method is :' + req.method)
    return render(req, 'index.html')

Django--FBV + CBV的更多相关文章

  1. Django FBV/CBV、中间件、GIT使用

    s5day82 内容回顾: 1. Http请求本质 Django程序:socket服务端 a. 服务端监听IP和端口 c. 接受请求 \r\n\r\n:请求头和请求体 \r\n & reque ...

  2. Django FBV CBV以及使用django提供的API接口

    FBV 和 CBV 使用哪一种方式都可以,根据自己的情况进行选择 看看FBV的代码 URL的写法: from django.conf.urls import url from api import v ...

  3. django FBV +CBV 视图处理方式总结

    1.FBV(function base views) 在视图里使用函数处理请求. url:        re_path('fbv', views.fbv),        # url(r'^fbv' ...

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

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

  5. django——FBV与CBV

    引言 FBV FBV(function base views) 就是在视图里使用函数处理请求. 在之前django的学习中,我们一直使用的是这种方式,所以不再赘述. CBV CBV(class bas ...

  6. Django的CBV与FBV

    FBV FBV(function base views) 就是在视图里使用函数处理请求. 在之前django的学习中,我们一直使用的是这种方式,所以不再赘述. CBV CBV(class base v ...

  7. Django的 CBV和FBV

    FBV CBV 回顾多重继承和Mixin 回到顶部 FBV FBV(function base views) 就是在视图里使用函数处理请求. 在之前django的学习中,我们一直使用的是这种方式,所以 ...

  8. Django 之 CBV & FBV

    FBV FBV(function base views) 就是在视图里使用函数处理请求. 在之前django随笔中,一直使用的是这种方式,不再赘述. CBV CBV(class base views) ...

  9. Django之CBV和FBV

    Django之CBV和FBV CBV和FBV是C和F的区别: C是Class,F是Function 在请求中,有GET请求和POST请求. 在写CBV时,url是可以对应一个类的,在类中,分别写出GE ...

  10. Django 路由视图FBV/CBV

    路由层  url路由层结构 from django.conf.urls import url from django.contrib import admin from app01 import vi ...

随机推荐

  1. 性能测试基础---jmeter参数化、关联、事物、检查的等

    ·Jmeter脚本增强·性能测试的脚本增强技术:参数化.关联.事务.检查点.思考时间和集合点. ·参数化:在Jmeter中,实现参数化的方式很多.本质上来说,参数化的实现方式有两种:·文件方式:一般建 ...

  2. curl-7.21.2

    curl 源码编译 自己定义的库编译 https://blog.csdn.net/initiallht/article/details/92655025 静态库,debug,x86nmake /f M ...

  3. 题解:洛谷P1891 疯狂LCM

    原题链接 题目描述 描述: 众所周知,czmppppp是数学大神犇.一天,他给众蒟蒻们出了一道数论题,蒟蒻们都惊呆了... 给定正整数N,求LCM(1,N)+LCM(2,N)+...+LCM(N,N) ...

  4. 【java异常】Unexpected error occurred in scheduled task. java.lang.StackOverflowError: null

    可能是栈溢出(StackOverFlow) 背景:我用定时器new东西 原因:频率太快了好像!

  5. 分布式文件系统HDFS

    利用Shell命令与HDFS进行交互 以”./bin/dfs dfs”开头的Shell命令方式 1.目录操作 在HDFS中为hadoop用户创建一个用户目录(hadoop用户) 启动hadoop 创建 ...

  6. 每日一问:简述 View 的绘制流程

    Android 开发中经常需要用一些自定义 View 去满足产品和设计的脑洞,所以 View 的绘制流程至关重要.网上目前有非常多这方面的资料,但最好的方式还是直接跟着源码进行解读,每日一问系列一直追 ...

  7. python 项目实战之logging日志打印

    官网介绍:https://docs.python.org/2/library/logging.html 一. 基础使用 1.1 logging使用场景 日志是什么?这个不用多解释.百分之九十的程序都需 ...

  8. 如何使用 Django中的 get_queryset, get_context_data和 get_object 等方法

    原文: https://blog.csdn.net/HH2030/article/details/80994274

  9. 注意:MagickReadImageBlob() 引发的问题

    今天发现: 如果之前的 mw 已加载了具体的图片数据后,再对这个 mw 进行: MagickReadImageBlob(mw, data, dataLen) 程序运行发生了崩溃. 最后找到原因: Ma ...

  10. Linux 就该这么学 CH07 使用RAID和LVM磁盘阵列技术

    1 RAID (独立冗余磁盘阵列) RAID 技术通过把多个硬盘设备组合成一个容量更大.安全性更好的磁盘阵列,并把数据切割成多个区段之后分别存在各个不同的物理硬盘设备上,然后利用分散读写计数来提升磁盘 ...