defdispatch(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)
重写 dispatch 方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
classLoginView(View): defdispatch(self, request, *args, **kwargs): print(11111) # print(request.META) #http所有相关请求头信息 ret = super().dispatch(request, *args, **kwargs) #render(request, 'login.html') print(2222) return ret defget(self,request): print('this is get method!!!') return render(request, 'login.html') defpost(self,request): uname = request.POST.get('username') pwd = request.POST.get('password') if uname == 'alex'and pwd == 'dsb': return redirect('/home/') else: return redirect('/login/')