Django【进阶篇】

本章内容

Class View 登录验证

  首页get方法登录验证,方法一

from django.utils.decorators import method_decorator

class IndexView(TemplateView):
    template_name = "index2.html"

    #首页需要登录验证,那就需要写上get方法
    @method_decorator(login_required())
    def get(self, request, *args, **kwargs):
        return super(IndexView, self).get(request, *args, **kwargs)

  方法二:

from django.contrib.auth.mixins import LoginRequiredMixin

class IndexView(LoginRequiredMixin, TemplateView):
    template_name = "index2.html"                   #这里不再需要自定义get的方法了,LoginRequiredMinin方法中实现
原文地址:https://www.cnblogs.com/nopnog/p/8316041.html