Fork me on GitHub

restframwork框架

一 APIView: 

class PublishView(APIView):

def get(self,request):
    publish_list=Publish.objects.all()
    ret=serialize("json",publish_list)

    return HttpResponse(ret)

def post(self,request):
    pass
#url(r'^publishes/', views.PublishView.as_view()),
#url(r'^publishes/', APIView.as_view()),
#url(r'^publishes/', APIView.as_view()),
#url(r'^publishes/', View.view),

 

当GET请求方法来的时候:

View.view(request):
    return self.dispatch(request, *args, **kwargs)
              ---- APIView.dispatch(request, *args, **kwargs):
    if request.method.lower() in self.http_method_names:
        handler = getattr(self, request.method.lower())

    response = handler(request, *args, **kwargs)
    return response

进行的其实是这四步:
#url(r'^publishes/', views.PublishView.as_view()),
#url(r'^publishes/', APIView.as_view()),
#url(r'^publishes/', APIView.as_view()),
#url(r'^publishes/', View.view),

 ① 

当请求走到:view = super(APIView, cls).as_view(**initkwargs)的时候,因为APIView本身继承的就是我们的View类。于是我们取执行View中的as_vi方法

 

 

② 执行APIView的父类View中的as_view()方法。

③ 执行APIView中的dispatch方法。

 

posted @ 2018-07-31 15:47  道阻切长  阅读(159)  评论(0编辑  收藏  举报