python测试开发django-73.django视图 CBV 和 FBV

前言

FBV(function base views) 就是在视图里使用函数处理请求,这一般是学django入门的时候开始使用的方式。
CBV(class base views) 就是在视图里使用类处理请求,这是面向对象的编程方式。

面试的时候基本上都是问到:你平常写的视图是基于函数的视图 (FBV),还是基于类的视图 (CBV),两者的区别在哪?
如果你只会写基于函数的视图,那说明还处于初级入门的阶段了。

FBV 模式

FBV(function base views)在views.py文件中定义视图函数来处理用户请求,函数中通过 if 判断 request.method 请求方式是 GET 还是 POST请求做对应的处理。

# views.py
from django.shortcuts import render
from django.http import HttpResponse, JsonResponse
# 上海悠悠,QQ交流群:750815713
# Create your views here.

# function base views

def fbvdemo(request):
    '''function base views'''
    context = {
        "code": 0,
        "msg": ""
    }
    if request.method == "GET":
        context["msg"] = "这是fbvdemo get请求"
        return JsonResponse(context)

    if request.method == "POST":
        context["msg"] = "这是fbvdemo POST请求"
        return JsonResponse(context)

urls.py配置访问路径

from django.conf.urls import url

urlpatterns = [

    url(r'^fbvdemo/$', views.fbvdemo)
]

CBV 模式

CBV(class base views) 就是在视图里使用类处理请求

  • 自定义的类必须继承 View 父类
  • 提高了代码的复用性,可以使用面向对象的技术,比如 Mixin(多继承)
  • 可以用不同的函数针对不同的 HTTP 方法处理,而不是通过很多if判断,提高代码可读性
  • CBV 模式继承的View类提供了多种请求方式对应的响应函数不需要在进行判断,可以直接在子类重写继承的方法
  • CBV 模式子类继承重写父类请求方式的响应函数通过父类 dispatch 方法进行反射分发
  • 在 urls.py 路由系统中必须使用 Myview.as_view() 替代视图函数
# views.py
from django.shortcuts import render
from django.http import HttpResponse, JsonResponse

# Create your views here.
# 上海悠悠,QQ交流群:750815713
# class base views

class Cbvdemo(View):
    context = {
        "code": 0,
        "msg": ""
    }

    def get(self, request):
        self.context["msg"] = "这是Cbvdemo get请求"
        return JsonResponse(self.context)

    def post(self, request):
        self.context["msg"] = "这是Cbvdemo post请求"
        return JsonResponse(self.context)

urls.py 配置访问路径

from django.conf.urls import url

urlpatterns = [

    url(r'^fbvdemo/$', views.fbvdemo),
    url(r'^cbvdemo/$', views.Cbvdemo.as_view()),
]

这2种方式从功能上讲都可以实现对应的功能,从代码的可读性来讲,建议多使用 CBV 模式。
另外通过 CBV 如果想要在执行get或post方法前执行其他步骤,可以重写dispatch。

猜你喜欢

转载自www.cnblogs.com/yoyoketang/p/11917734.html