1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| url(r'^login/', views.Mylogin.as_view()) '''类名点名字还加括号 名字要么是绑定给类的方法 要么是无参函数'''
1.as_view()绑定给类的方法 @classonlymethod def as_view(cls, **initkwargs) 2.CBV路由匹配本质与FBV一致 # CBV url(r'^login/', views.Mylogin.as_view()) # CBV本质 # url(r'^login/', views.view) 3.匹配成功之后执行view函数代码 def view(request, *args, **kwargs): self = cls(**initkwargs) return self.dispatch(request, *args, **kwargs) 4.查看dispatch方法(对象查找属性和方法一定要严格按照顺序来) def dispatch(self, request, *args, **kwargs): 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)
|