Django fbv 和 cbv 简述

简介:

前面学习的例子都是通过 url 来匹配 一个函数,这种方式叫做 function based view (BSV)。一个典型的使用方式就是通过在view.py里面定义一个函数,然后通过函数的request参数获取method的类型,比如直接刷新页面就是get方式,提交表单就是post方式,这样来根据提交的不同方式进行不同的处理。

比如:

1
2
3
4
5
6
7
8
9
10
def  user_info(request):
     if  request.method  = =  "GET" :
         user_list  =  models.UserInfo.objects. all ()
         group_list  =  models.UserGroup.objects. all ()
         return  render(request,  'user_info.html' , { 'user_list' : user_list,  "group_list" : group_list})
     elif  request.method  = =  'POST' :
         =  request.POST.get( 'user' )
         =  request.POST.get( 'pwd' )
         models.UserInfo.objects.create(username = u,password = p)
         return  redirect( '/cmdb/user_info/' )

  

另外一种方式叫做 class based view (CBV)。这种方式需要导入一个View类,通过自定义一个子类继承这个类,我们可以利用View里面的dispatch()方法来判断请求是get还是post。dispatch方法的本质是一个反射器,可以根据名字来调用同名的方法。


View类的部分代码可以看出dispatch通过反射获取列表里面的方法名称

1
2
3
4
5
6
7
8
9
10
   http_method_names  =  [ 'get' 'post' 'put' 'patch' 'delete' 'head' 'options' 'trace' ]
   def  dispatch( self , request,  * args,  * * kwargs):
         # Try to dispatch to the right method; if a method doesn't exist,
         # defer to the error handler. Also defer to the error handler if the
         # request method isn't on the approved list.
         if  request.method.lower()  in  self .http_method_names:
             handler  =  getattr ( self , request.method.lower(),  self .http_method_not_allowed)
         else :
             handler  =  self .http_method_not_allowed
         return  handler(request,  * args,  * * kwargs)


CBV的例子如下。


注意这里在子类里面重新定义了dispatch这个方法,同时又使用了super()来调用父类的构造方法,这样的好处是功能不变,但是可以灵活的添加新的功能。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
from  django.views  import  View
class  Home(View):
     def  dispatch( self , request,  * args,  * * kwargs):
         # 调用父类中的dispatch
         print ( 'before' )
         result  =  super (Home, self ).dispatch(request,  * args,  * * kwargs)
         print ( 'after' )
         return  result
         
     def  get( self ,request):
         print (request.method)
         return  render(request,  'home.html' )
         
     def  post( self ,request):
         print (request.method, 'POST' )
         return  render(request,  'home.html' )


为了调用自定义的类里面的方法,在url里面,我们需要调用View类的as_view()方法作为入口。

比如说

1
2
3
4
5
6
7
8
# urls.py
from  django.conf.urls  import  url
from  myapp.views 
import  MyView
urlpatterns  =  [
     
     url(r '^about/' , MyView.as_view()),
]






本文转自 beanxyz 51CTO博客,原文链接:http://blog.51cto.com/beanxyz/1944732,如需转载请自行联系原作者

目录
相关文章
Django-视图CBV执行流程详解
FBV和CBV其实就是要么写函数要么写类,那么内部原理是怎么实现? FBV写法 def func_view(request): "Function views" if request.
1502 0
|
JSON 数据格式 Python
Django之CBV
CBV,即Class Base View,类基本视图。 在写API时,我们通常都是使用CBV,而非FBV (Function Base View)。 1.
1422 0
|
Web App开发 Go Python
源码解析Django CBV的本质
Django CBV模式的源码解析 通常来说,http请求的本质就是基于Socket Django的视图函数,可以基于FBV模式,也可以基于CBV模式。 基于FBV的模式就是在Django的路由映射表里进行url和视图函数的关联,而基于CBV的模式则是在views.
1372 0
第05章节-Python3.5-Django的CBV和FBV 4
4. FBV & CBV function base view url.py index -> 函数名 view.py def 函数(request): .
1320 0
|
数据安全/隐私保护 Python
|
Python
如何在DJANGO里获取?带数据的东东,基于CBV
用DEF的,有现成的,而用CLASS的,就要作一下变通。 如下: if self.request.GET: if self.request.GET.get('search_pk'): search_pk = self.
940 0
|
5天前
|
运维 监控 Serverless
Serverless 应用引擎产品使用之阿里函数计算中在自定义环境下用debian10运行django,用官方层的python3.9,配置好环境变量后发现自定义层的django找不到了如何解决
阿里云Serverless 应用引擎(SAE)提供了完整的微服务应用生命周期管理能力,包括应用部署、服务治理、开发运维、资源管理等功能,并通过扩展功能支持多环境管理、API Gateway、事件驱动等高级应用场景,帮助企业快速构建、部署、运维和扩展微服务架构,实现Serverless化的应用部署与运维模式。以下是对SAE产品使用合集的概述,包括应用管理、服务治理、开发运维、资源管理等方面。
14 3