Loading

3.CBV视图之csrf补充

CBV使用csrf装饰器关闭/开启 csrf验证,直接在函数上加装饰器无效的
#方法1
from django.views import View
from django.views.decorators.csrf import csrf_exempt, csrf_protect
from django.utils.decorators import method_decorator


class Login(View):
   #在视图类中函数上加装饰器是无效的,必须放在dispatch中【源码中反射处理的函数】
   @method_decorator(csrf_exempt)
   def dispatch(self, request, *args, **kwargs):
       return super(StudentsView,self).dispatch(request, *args, **kwargs)
    
    def users(self):
        pass
        
    def inofs(self):
        pass
#方法2 推荐
from django.views import View
from django.views.decorators.csrf import csrf_exempt, csrf_protect
from django.utils.decorators import method_decorator
 
#csrf处理方式,指向源码中的dispatch
@method_decorator(csrf_protect,name='dispatch')
class Login(View):
    def users(self):
        pass

    def inofs(self):
        pass

 

posted @ 2022-01-13 17:33  木子七  阅读(29)  评论(0编辑  收藏  举报