restframework api(基础3CBV)

一 CBV源码流程

urls.py

from django.conf.urls import url
from django.contrib import admin
from app01 import views

urlpatterns = [
    url(r‘^order/‘, views.OrderView.as_view()),
]

  

view.py

from django.shortcuts import render
from django.http import JsonResponse
from django.views import View
class OrderView(View):

    def get(self,request,*args,**kwargs):
        ret = {‘code‘: 1000, ‘msg‘: None, ‘error‘: None}
        return JsonResponse(ret)

  

1)从上面的urls.py文件种可以看到,一个url对应了一个  这个views.OrderView.as_view()函数,并执行这个函数,也就是我们调用order的url会views.OrderView.as_view()()

2)从views.py文件种看到OrderView这个类种,并没有as_view()方法,所以需要到View类种找

    # 这是一个类方法
    @classonlymethod
    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 = OrderView()
            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
            # handler = getattr(self, request.method.lower(), self.http_method_not_allowed)
            # return handler(request, *args, **kwargs)
            # 这个as_view()返回了一个OrderView().dispatch(request, *args, **kwargs)方法
            # 这里返回了dispatch(),所以这个dispath方法会被执行,但是需要从头开始找起
            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=())
        # 由于调用url会views.OrderView.as_view()()所以,这里会执行view函数
        return view

    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
        # 最终返回了执行后的handler()
        return handler(request, *args, **kwargs)

  

原文地址:https://www.cnblogs.com/wanstack/p/9058012.html

时间: 2024-08-30 16:10:14

restframework api(基础3CBV)的相关文章

Jersey API基础及应用例子

概述:在学习微信接口的过程中用到了请求web接口资源的操作 Jersey 客户端API基础 首先需要创建一个com.sun.jersey .api.client.Client 类的实例. import com.sun.jersey .api.client.Client; Client client = Client.create(); 或者 ClientConfig clientConfig = ClienConfig.DefaultClientConfig(); Client client =

三维引擎设计-多线程渲染(平台API基础和封装大致框架)

第一部分: Linux线程API基础 一:线程创建与结束 (1)pthread_t //线程的标识符类型 (2)pthread_create //用来创建一个线程, 参数线程标识符, 线程属性, 线程运行函数地址 (3)pthread_join //用来等待一个线程的结束, 参数被等待线程标识符,用户自定义指针 (4)pthread_exit //线程非正常结束,参数线程返回代码 二:修改线程属性 (1)pthread_attr_t //线程属性结构类型 (2)pthread_attr_init

【转载】从头编写 asp.net core 2.0 web api 基础框架 (1)

工具: 1.Visual Studio 2017 V15.3.5+ 2.Postman (Chrome的App) 3.Chrome (最好是) 关于.net core或者.net core 2.0的相关知识就不介绍了, 这里主要是从头编写一个asp.net core 2.0 web api的基础框架. 我一直在关注asp.net core 和 angular 2/4, 并在用这对开发了一些比较小的项目. 现在我感觉是时候使用这两个技术去为企业开发大一点的项目了, 由于企业有时候需要SSO(单点登

从头编写 asp.net core 2.0 web api 基础框架 (4) EF配置

原文:从头编写 asp.net core 2.0 web api 基础框架 (4) EF配置 第1部分:http://www.cnblogs.com/cgzl/p/7637250.html 第2部分:http://www.cnblogs.com/cgzl/p/7640077.html 第3部分:http://www.cnblogs.com/cgzl/p/7652413.html Github源码地址:https://github.com/solenovex/Building-asp.net-co

从头编写 asp.net core 2.0 web api 基础框架 (5) EF CRUD

原文:从头编写 asp.net core 2.0 web api 基础框架 (5) EF CRUD 第1部分:http://www.cnblogs.com/cgzl/p/7637250.html 第2部分:http://www.cnblogs.com/cgzl/p/7640077.html 第3部分:http://www.cnblogs.com/cgzl/p/7652413.html 第4部分:http://www.cnblogs.com/cgzl/p/7661805.html Github源码

从头编写 asp.net core 2.0 web api 基础框架 (3)

原文:从头编写 asp.net core 2.0 web api 基础框架 (3) 第一部分:http://www.cnblogs.com/cgzl/p/7637250.html 第二部分:http://www.cnblogs.com/cgzl/p/7640077.html Github源码地址:https://github.com/solenovex/Building-asp.net-core-2-web-api-starter-template-from-scratch 之前我介绍完了asp

从头编写 asp.net core 2.0 web api 基础框架 (2)

原文:从头编写 asp.net core 2.0 web api 基础框架 (2) 上一篇是: http://www.cnblogs.com/cgzl/p/7637250.html Github源码地址是: https://github.com/solenovex/Building-asp.net-core-2-web-api-starter-template-from-scratch 本文讲的是里面的Step 2. 上一次, 我们使用asp.net core 2.0 建立了一个Empty pr

从头编写 asp.net core 2.0 web api 基础框架 (1)

原文:从头编写 asp.net core 2.0 web api 基础框架 (1) 工具: 1.Visual Studio 2017 V15.3.5+ 2.Postman (Chrome的App) 3.Chrome (最好是) 关于.net core或者.net core 2.0的相关知识就不介绍了, 这里主要是从头编写一个asp.net core 2.0 web api的基础框架. 我一直在关注asp.net core 和 angular 2/4, 并在用这对开发了一些比较小的项目. 现在我感

restframework api(基础2)

本篇基于http://www.cnblogs.com/wanstack/p/9008872.html 下面主要是整理一下django的相关知识,希望能有意外的收获. 看到了一篇好的文章,忍不住的想要抄袭下来... 原文地址https://www.jianshu.com/p/17d8266bb265 下面开始我的抄袭之路... 原文地址:https://www.cnblogs.com/wanstack/p/9025964.html