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

  • 相关阅读:
    WinForm多线程+委托防止界面假死
    C#中异步及winform中界面假死
    js如何判断当前文本的输入状态——中文输入法的那些坑
    数据库主键设计之思考
    node.js使用superagent实现模拟登陆功能(包含下载验证码功能)
    application.properties在Spring Boot项目中的位置
    ubuntu18.04安装chromium浏览器
    ubuntu 18.04安装mysql 8
    Spring Boot学习笔记——搭建一个最简单的hello world
    ubuntu 18.04安装jdk8和eclipse
  • 原文地址:https://www.cnblogs.com/liqianglog/p/11133212.html
Copyright © 2011-2022 走看看