通常来说,http请求的本质就是基于Socket

Django的视图函数,可以基于FBV模式,也可以基于CBV模式。

基于FBV的模式就是在Django的路由映射表里进行url和视图函数的关联,而基于CBV的模式则是在views.py文件中定义视图类,在视图类中视图函数,如get,post,put,delete等

使用Django新建一个项目,新建一个路由映射

  1. from django.conf.urls import url
  2. from django.contrib import admin
  3. from app01 import views
  4. urlpatterns = [
  5. url(r\'^cbv/$\',views.CBV.as_view())
  6. ]

对应的views.py文件内容:

  1. from django.shortcuts import render,HttpResponse
  2. from django.views import View
  3. class CBV(View):
  4. def get(self,request):
  5. return HttpResponse("GET")
  6. def post(self,request):
  7. return HttpResponse("POST")

启动项目,使用浏览器请求URLhttp://127.0.0.1:8000/cbv/,浏览器显示结果为:

请求到达Django会先执行Django中间件里的方法,然后进行进行路由匹配。

在路由匹配完成后,会执行CBV类中的as_view方法。

CBV中并没有定义as_view方法,由于CBV继承自Django的View,所以会执行Django的View类中的as_view方法

Django的View类的as_view方法的部分源码

  1. class View(object):
  2. """
  3. Intentionally simple parent class for all views. Only implements
  4. dispatch-by-method and simple sanity checking.
  5. """
  6. http_method_names = [\'get\', \'post\', \'put\', \'patch\', \'delete\', \'head\', \'options\', \'trace\']
  7. def __init__(self, **kwargs):
  8. """
  9. Constructor. Called in the URLconf; can contain helpful extra
  10. keyword arguments, and other things.
  11. """
  12. # Go through keyword arguments, and either save their values to our
  13. # instance, or raise an error.
  14. for key, value in six.iteritems(kwargs):
  15. setattr(self, key, value)
  16. @classonlymethod
  17. def as_view(cls, **initkwargs):
  18. """
  19. Main entry point for a request-response process.
  20. """
  21. for key in initkwargs:
  22. if key in cls.http_method_names:
  23. raise TypeError("You tried to pass in the %s method name as a "
  24. "keyword argument to %s(). Don\'t do that."
  25. % (key, cls.__name__))
  26. if not hasattr(cls, key):
  27. raise TypeError("%s() received an invalid keyword %r. as_view "
  28. "only accepts arguments that are already "
  29. "attributes of the class." % (cls.__name__, key))
  30. def view(request, *args, **kwargs):
  31. self = cls(**initkwargs)
  32. if hasattr(self, \'get\') and not hasattr(self, \'head\'):
  33. self.head = self.get
  34. self.request = request
  35. self.args = args
  36. self.kwargs = kwargs
  37. return self.dispatch(request, *args, **kwargs)
  38. view.view_class = cls
  39. view.view_initkwargs = initkwargs
  40. # take name and docstring from class
  41. update_wrapper(view, cls, updated=())
  42. # and possible attributes set by decorators
  43. # like csrf_exempt from dispatch
  44. update_wrapper(view, cls.dispatch, assigned=())
  45. return view

从View的源码可以看出,在View类中,先定义了http请求的八种方法

  1. http_method_names = [\'get\', \'post\', \'put\', \'patch\', \'delete\', \'head\', \'options\', \'trace\']

as_view方法中进行判断,如果请求的方法没在http_method_names中,则会抛出异常,这里的cls实际上指的是自定义的CBV类

接着as_view方法中又定义view方法,在view方法中对CBV类进行实例化,得到self对象,然后在self对象中封装浏览器发送的request请求

  1. self = cls(**initkwargs)

最后又调用了self对象中的dispatch方法并返回dispatch方法的值来对request进行处理

此时,由于self对象就是CBV实例化得到,所以会先执行自定义的CBV类中的dispatch方法。如果CBV类中没有定义dispatch方法则执行Django的View中的dispatch方法

Django的View中的dispatch方法源码

  1. class View(object):
  2. """
  3. 中间省略
  4. """
  5. def dispatch(self, request, *args, **kwargs):
  6. # Try to dispatch to the right method; if a method doesn\'t exist,
  7. # defer to the error handler. Also defer to the error handler if the
  8. # request method isn\'t on the approved list.
  9. if request.method.lower() in self.http_method_names:
  10. handler = getattr(self, request.method.lower(), self.http_method_not_allowed)
  11. else:
  12. handler = self.http_method_not_allowed
  13. return handler(request, *args, **kwargs)

在dispatch方法中,把request.method转换为小写再判断是否在定义的http_method_names中,如果request.method存在于http_method_names中,则使用getattr反射的方式来得到handler

在这里的dispatch方法中,self指的是自定义的CBV类实例化得到的对象

从CBV类中获取request.method对应的方法,再执行CBV中的方法并返回

由此,可以知道如果在Django项目中使用CBV的模式,实际上调用了getattr的方式来执行获取类中的请求方法对应的函数

结论:

  1. CBV基于反射实现根据请求方式不同,执行不同的方法

如果想在基于CBV模式的项目中在请求某个url时执行一些操作,则可以在url对应的类中定义dispatch方法

修改views.py文件

  1. class CBV(View):
  2. def dispatch(self, request, *args, **kwargs):
  3. func = getattr(self,request.method.lower())
  4. return func(request,*args,**kwargs)
  5. def get(self,request):
  6. return HttpResponse("GET")
  7. def post(self,request):
  8. return HttpResponse("POST")

也可以使用继承的方式重写dispatch方法:

  1. class CBV(View):
  2. def dispatch(self, request, *args, **kwargs):
  3. print("before")
  4. res = super(CBV, self).dispatch(request, *args, **kwargs)
  5. print("after")
  6. return res
  7. def get(self,request):
  8. return HttpResponse("GET")
  9. def post(self,request):
  10. return HttpResponse("POST")

刷新浏览器,Django后台打印结果如下:

浏览器页面结果

同理,如果有基于CBV的多个类,并且有多个类共用的功能,为了避免重复,可以单独定义一个类,在这个类中重写dispatch方法,然后让url对应的视图类继承这个类

修改urls.py文件

  1. from django.conf.urls import url
  2. from django.contrib import admin
  3. from app01 import views
  4. urlpatterns = [
  5. url(r\'^cbv1/$\',views.CBV1.as_view()),
  6. url(r\'^cbv2/$\',views.CBV2.as_view()),
  7. ]

views.py文件内容

  1. from django.shortcuts import render,HttpResponse
  2. from django.views import View
  3. class BaseView(object):
  4. def dispatch(self, request, *args, **kwargs):
  5. func = getattr(self, request.method.lower())
  6. return func(request, *args, **kwargs)
  7. class CBV1(BaseView,View):
  8. def get(self,request):
  9. return HttpResponse("CBV1 GET")
  10. def post(self,request):
  11. return HttpResponse("CBV1 POST")
  12. class CBV2(BaseView,View):
  13. def get(self,request):
  14. return HttpResponse("CBV2 GET")
  15. def post(self,request):
  16. return HttpResponse("CBV2 POST")

通过python的面向对象可以知道,请求到达视图类时,会先执行CBV1和CBV2类中的dispatch方法,然而CBV1和CBV2类中并没有dispatch方法,则会按照顺序在父类中查找dispatch方法,此时就会执行BaseView类中的dispatch方法了

用浏览器请求urlhttp://127.0.0.1:8000/cbv1/,浏览器页面显示

用浏览器请求urlhttp://127.0.0.1:8000/cbv2/,浏览器页面显示

版权声明:本文为renpingsheng原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/renpingsheng/p/9531649.html