django类视图的装饰器验证

时间:2023-03-09 16:34:10
django类视图的装饰器验证

django类视图的装饰器验证


  • django类视图的get和post方法是由View内部调用dispatch方法来分发,最后调用as_view来完成一个视图的流程。
  • 函数视图可以直接使用对应的装饰器
  • 类视图可以用MixIn的方法来对类视图的dispatch或as_view方法对一个的封装
  • django提供了一个method_decorator的装饰器可以直接对类视图的处理方法进行装饰
  • from django.utils.decorators import method_decorator
    from django.contrib.auth.decorators import login_required
    from django.views import View
    from django.http import HttpResponse class TestView(View): @method_decorator(login_required)
    def get(self, request):
    return HttpResponse('hello world')