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

时间:2025-05-04 10:36:32

前言

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。

login_requierd登录校验

FBV 模式如果需要加登录之后才能访问,只需在函数上加装饰器@login_required

from django.contrib.auth.decorators import login_required

# function base views

@login_required
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)

没加装饰器之前,可以直接访问

C:\Users\dell>http http://localhost:8000/fbvdemo/
HTTP/1.1 200 OK
Content-Length: 55
Content-Type: application/json
Date: Sat, 23 Nov 2019 06:26:58 GMT
Server: WSGIServer/0.2 CPython/3.6.0
Vary: Origin
X-Frame-Options: SAMEORIGIN {
"code": 0,
"msg": "这是个get请求"
}

加了@login_required装饰器后,没登录的话会302重定向到登录页 /accounts/login/

C:\Users\dell>http http://localhost:8000/fbvdemo/
HTTP/1.1 302 Found
Content-Length: 0
Content-Type: text/html; charset=utf-8
Date: Sat, 23 Nov 2019 06:27:19 GMT
Location: /accounts/login/?next=/fbvdemo/
Server: WSGIServer/0.2 CPython/3.6.0
Vary: Origin, Cookie
X-Frame-Options: SAMEORIGIN

CBV 模式加 login_requierd 登录校验

method_decorator给CBV视图添加登录校验。

from django.utils.decorators import method_decorator
from django.contrib.auth.decorators import login_required
# 上海-悠悠,QQ交流群:750815713 class Cbvdemo(View):
context = {
"code": 0,
"msg": ""
} @method_decorator(login_required)
def get(self, request):
self.context["msg"] = "这是Cbvdemo get请求"
return JsonResponse(self.context) @method_decorator(login_required)
def post(self, request):
self.context["msg"] = "这是Cbvdemo post请求"
return JsonResponse(self.context)

也可以直接在class上用装饰器

from django.utils.decorators import method_decorator
from django.contrib.auth.decorators import login_required @method_decorator(login_required, name='get')
@method_decorator(login_required, name='post')
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)