python框架之Django(8)-CBV中添加裝飾器

現有以下檢查登陸裝飾器: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
Code

使用

要在CBV視圖中使用咱們上面的check_login裝飾器,有如下三種方式:django

加在的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/")
Code

加在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/")
Code

由於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/")
Code

補充

CSRF Token相關裝飾器

  • csrf_protect

    爲當前函數強制設置防跨站請求僞造功能,即使settings中沒有設置全局中間件。函數

  • csrf_exempt

    取消當前函數防跨站請求僞造功能,即使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/")
    加到dispatch方法上
     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/")
    加在視圖類上
相關文章
相關標籤/搜索