CBV源码分析
查看源码的方式,先查看自身,没有去找父类,父类没有就去找父父类。。。
自己定义的类
class Author(View): def get(self,request): back_dic = {'status_code': 100, 'msg': 'get ok'} return JsonResponse(back_dic) def post(self,request): back_dic = {'status_code': 102, 'msg': 'post ok'} return JsonResponse(back_dic)
我们看到自己定义的类中并没有这个as_view()方法,那么他只能是从我们继承的父类View中来的,那我们就点到View里面去看有没有as_view()这个方法
刚好就在这个里面,然后点开这个方法,看里面具体做了什么操作
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
def as_view(cls, **initkwargs): """ Main entry point for a request-response process. """ for key in initkwargs: if key in cls.http_method_names: raise TypeError("You tried to pass in the %s method name as a " "keyword argument to %s(). Don't do that." % (key, cls.__name__)) if not hasattr(cls, key): raise TypeError("%s() received an invalid keyword %r. as_view " "only accepts arguments that are already " "attributes of the class." % (cls.__name__, key)) def view(request, *args, **kwargs): self = cls(**initkwargs) if hasattr(self, 'get') and not hasattr(self, 'head'): self.head = self.get self.request = request self.args = args self.kwargs = kwargs return self.dispatch(request, *args, **kwargs) view.view_class = cls view.view_initkwargs = initkwargs # take name and docstring from class update_wrapper(view, cls, updated=()) # and possible attributes set by decorators # like csrf_exempt from dispatch update_wrapper(view, cls.dispatch, assigned=()) return view
简单分析,如图
我们看到,调用了as_view()实际上是走了view方法,然后再view方法中给我们返回了一个对象的dispatch方法,那么我们就得搞清楚,现在的self是谁~
self是View的对象,然后我们自定义的类继承了View,所以说self就是我们自己定义类的对象,那么就简单了,我们先去自己定义的类中找我们有没有写dispatch方法,没有再去找父类
父类中dispatch方法源码
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)
源码分析
此时的handler就是我们发送的请求方式,比如GET,但是是小写的而已,然后再自己定义的类中与get对应上了,所以就会get请求走get,post请求走post。
此处的精髓通过反射取到请求方式