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。
网友评论