美文网首页django
django:dispatch and handler

django:dispatch and handler

作者: dddddo | 来源:发表于2019-12-07 21:02 被阅读0次
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)

一个getattr的例子

class person():
    name = "Zhang"
    age = 18
t = test()
print(getattr(t, "name", "There is no such property"))               

handler = getattr(self, request.method.lower(), self.http_method_not_allowed) 的结果是得到一个request.method, 例如post,get等等。
而return handler(request, *args, **kwargs)将参数(request, *args, **kwargs)输入request.method。

相关文章

网友评论

    本文标题:django:dispatch and handler

    本文链接:https://www.haomeiwen.com/subject/cejugctx.html