dispatch 方法可以直接在用户请求之前在一些事情,例如计算请求耗时,判断客户端请求头

    1. class LoginView(View):
    2. def dispatch(self, request, *args, **kwargs):
    3. print('开始请求---->')
    4. ret = super().dispatch(request,*args,**kwargs)
    5. print('请求结束----->')
    6. return ret
    7. def get(self,request):
    8. print('get')
    9. return render(request,'login.html')
    10. def post(self,request):
    11. print('post')
    12. username = request.POST.get('username')
    13. password = request.POST.get('password')
    14. return HttpResponse(username+password)

    image.png