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')

     

 

posted @ 2019-09-02 14:39  Ivy丶  阅读(339)  评论(0)    收藏  举报