• 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类中的其他操作。


  • 相关阅读:
    05 库的简单操作
    04 基本的MySQL语句
    03 MySQL安装和基本管理
    02 数据库概述
    01 MySQL入门了解
    Django-组件拾遗
    bootstrap基础讲解
    jQuery练习
    前端基础之Jquery
    15 Django组件-中间件
  • 原文地址:https://www.cnblogs.com/bbjs/p/12933756.html
Copyright © 2020-2023  润新知