现有以下检查登陆装饰器:html
1 from functools import wraps 2 3 4 def check_login(func): 5 @wraps(func) 6 def inner(request, *args, **kwargs): 7 next_url = request.get_full_path() 8 if request.session.get("user"): 9 return func(request, *args, **kwargs) 10 else: 11 return redirect("/login/?next={}".format(next_url)) 12 return inner
要在CBV视图中使用咱们上面的check_login装饰器,有如下三种方式:django
1 from django.utils.decorators import method_decorator 2 3 4 class HomeView(View): 5 6 def dispatch(self, request, *args, **kwargs): 7 return super(HomeView, self).dispatch(request, *args, **kwargs) 8 9 def get(self, request): 10 return render(request, "home.html") 11 12 @method_decorator(check_login) 13 def post(self, request): 14 print("Home View POST method...") 15 return redirect("/index/")
1 from django.utils.decorators import method_decorator 2 3 4 class HomeView(View): 5 6 @method_decorator(check_login) 7 def dispatch(self, request, *args, **kwargs): 8 return super(HomeView, self).dispatch(request, *args, **kwargs) 9 10 def get(self, request): 11 return render(request, "home.html") 12 13 def post(self, request): 14 print("Home View POST method...") 15 return redirect("/index/")
由于CBV中首先执行的就是dispatch方法,因此这么写至关于给get和post方法都加上了登陆校验。session
直接加在视图类上,但method_decorator必须传name关键字参数。若是get方法和post方法都须要登陆校验的话就写两个装饰器。ide
1 from django.utils.decorators import method_decorator 2 3 @method_decorator(check_login, name="get") 4 @method_decorator(check_login, name="post") 5 class HomeView(View): 6 7 def dispatch(self, request, *args, **kwargs): 8 return super(HomeView, self).dispatch(request, *args, **kwargs) 9 10 def get(self, request): 11 return render(request, "home.html") 12 13 def post(self, request): 14 print("Home View POST method...") 15 return redirect("/index/")
为当前函数强制设置防跨站请求伪造功能,即使settings中没有设置全局中间件。函数
取消当前函数防跨站请求伪造功能,即使settings中设置了全局中间件。post
CSRF Token相关装饰器在CBV只能加到dispatch方法上,或者加在视图类上而后name参数指定为dispatch方法。url
1 from django.views.decorators.csrf import csrf_exempt, csrf_protect 2 from django.utils.decorators import method_decorator 3 4 5 class HomeView(View): 6 7 @method_decorator(csrf_exempt) 8 def dispatch(self, request, *args, **kwargs): 9 return super(HomeView, self).dispatch(request, *args, **kwargs) 10 11 def get(self, request): 12 return render(request, "home.html") 13 14 def post(self, request): 15 print("Home View POST method...") 16 return redirect("/index/")
1 from django.views.decorators.csrf import csrf_exempt, csrf_protect 2 from django.utils.decorators import method_decorator 3 4 5 @method_decorator(csrf_exempt, name='dispatch') 6 class HomeView(View): 7 8 def dispatch(self, request, *args, **kwargs): 9 return super(HomeView, self).dispatch(request, *args, **kwargs) 10 11 def get(self, request): 12 return render(request, "home.html") 13 14 def post(self, request): 15 print("Home View POST method...") 16 return redirect("/index/")