django14:CBV加入装饰器
生活随笔
收集整理的這篇文章主要介紹了
django14:CBV加入装饰器
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
加在方法上面
from django.utils.decorators import method_decoratorclass HomeView(View):def dispatch(self, request, *args, **kwargs):return super(HomeView, self).dispatch(request, *args, **kwargs)def get(self, request):return render(request, "home.html")@method_decorator(check_login)def post(self, request):print("Home View POST method...")return redirect("/index/")?
加在class上面,可多個
from django.utils.decorators import method_decorator@method_decorator(check_login, name="get")@method_decorator(check_login, name="post")class HomeView(View):def dispatch(self, request, *args, **kwargs):return super(HomeView, self).dispatch(request, *args, **kwargs)def get(self, request):return render(request, "home.html")def post(self, request):print("Home View POST method...")return redirect("/index/")?
加在dispatch上
from django.utils.decorators import method_decoratorclass HomeView(View):@method_decorator(check_login)def dispatch(self, request, *args, **kwargs):return super(HomeView, self).dispatch(request, *args, **kwargs)def get(self, request):return render(request, "home.html")def post(self, request):print("Home View POST method...")return redirect("/index/")參考:https://www.shuzhiduo.com/A/Vx5MOX43zN/
總結
以上是生活随笔為你收集整理的django14:CBV加入装饰器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: django13:Session与Coo
- 下一篇: django15:中间件