python框架之Django(8)-CBV中添加装饰器
现有如下检查登录装饰器:
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装饰器,有以下三种方式:
加在的get或post方法上
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/")
加在dispatch方法上
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方法都加上了登录校验。
加在视图类上
直接加在视图类上,但method_decorator必须传name关键字参数。如果get方法和post方法都需要登录校验的话就写两个装饰器。
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/")
补充
CSRF Token相关装饰器
-
csrf_protect
为当前函数强制设置防跨站请求伪造功能,即便settings中没有设置全局中间件。
-
csrf_exempt
取消当前函数防跨站请求伪造功能,即便settings中设置了全局中间件。
-
使用
CSRF Token相关装饰器在CBV只能加到dispatch方法上,或者加在视图类上然后name参数指定为dispatch方法。
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/")
Java博客目录 | Python博客目录 | C#博客目录