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分发请求方式并返回函数执行结果

  • 相关阅读:
    嵌入式框架Zorb Framework搭建三:列表的实现
    嵌入式框架Zorb Framework搭建二:环形缓冲区的实现
    C#比较两个时间大小
    大数据概述
    综合练习:英文词频统计
    熟悉常用的Linux操作
    Python基础画五星红旗
    字符串、组合数据类型练习
    简化版C语言文法
    词法分析实验报告
  • 原文地址:https://www.cnblogs.com/liqianglog/p/11133212.html
Copyright © 2011-2022 走看看