加在方法上面
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/