V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
callmehongpeng
V2EX  ›  问与答

Django 中视图调用 as_view 方法的一些问题

  •  
  •   callmehongpeng · Jul 21, 2018 · 1470 views
    This topic created in 2848 days ago, the information mentioned may be changed or developed.

    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
    

    ====================================================== 如上所示源代码,最后 return view 为什么会调用内部 view 方法 从而 return dispatch(..) return 方法名字不应该返回的是内存地址嘛= = 新人求教(づ ̄ 3  ̄)づ╭❤~

    1 replies    2018-07-21 10:58:54 +08:00
    callmehongpeng
        1
    callmehongpeng  
    OP
       Jul 21, 2018
    哦吼吼,明白了 是浏览器给调用的 o(╥﹏╥)o
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   994 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 19:43 · PVG 03:43 · LAX 12:43 · JFK 15:43
    ♥ Do have faith in what you're doing.