zoukankan      html  css  js  c++  java
  • 1. Django每日一码 之原生View源码

    2019-7-4  今日源码:原生 View

    1.URL中调用as_view方法

        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

    最终返回view,说明实际上我们url执行的是view方法

    2.view函数

            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)

    最终执行分发函数dispatch

    3.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)

    分发url执行get/post/put等方法

    流程总结

    1.as_view()返回view函数
    2.view函数返回dispacth的执行结果
    3.dispacth分发请求方式并返回函数执行结果

  • 相关阅读:
    软件架构学习小结
    20+ 个很有用的 jQuery 的 Google 地图插件 (英语)
    网页JS获取当前地理位置(省市区)
    前端Js框架汇总(工具多看)
    MUI简介-最接近原生App体验的前端框架
    Bootstrap手机网站开发案例
    jQuery Mobile手机网站案例
    历届图灵奖 (Turing award)得奖名单
    js进阶 10-9 -of-type型子元素伪类选择器
    网页如何实现隔多久自动调用某个方法
  • 原文地址:https://www.cnblogs.com/liqianglog/p/11133212.html
Copyright © 2011-2022 走看看