LoginRequiredMixin类

时间:2020-05-21
本文章向大家介绍LoginRequiredMixin类,主要包括LoginRequiredMixin类使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
from django.contrib.auth.decorators import login_required
class LoginRequiredMixin():
    @classmethod
    def as_view(cls):
        view = super().as_view()
        return login_required(view)

from utils.mixin import LoginRequiredMixin
class OrderPlaceView(LoginRequiredMixin, View):
     可以看到,在LoginRequiredMixin类中并没有as_view方法,而且它只是继承了object,也没有as_view方法,但是它的子类中是多继承,在View中有as_view()方法,所以LoginRequiredMixin使用了View类中的as_view方法。
下面在来一段简单的代码理接:
class Father():
    def view(self):
        print("this is Father's CLass")

class Mother():    
    def view(self):
        super().view()
        print("this's Mother")

class Son(Mother,Father):
    def view(self):
        super().view()
        print('Son')
son = Son()
son.view()

输出
this is Father's CLass
this's Mother
Son


创建Son对象,执行view方法,方法继承自上一级类,找Mother,有,进去,发现没有具体实现,
找上一级的类Father,有具体实现,执行Mother中的其他操作,执行Son类中的其他操作。


原文地址:https://www.cnblogs.com/bbjs/p/12933756.html