Python Django:在基于类的视图中添加另一个子类

Python Django:在基于类的视图中添加另一个子类,python,django,django-class-based-views,Python,Django,Django Class Based Views,这是我的第一个django应用程序,我想知道是否有可能有一个通用类,它将被所有视图扩展。比如说 class GeneralParent: def __init__(self): #SETTING Up different variables self.LoggedIn = false def isLoggedIn(self): return self.LoggedIn class FirstView(TemplateView): #

这是我的第一个django应用程序,我想知道是否有可能有一个通用类,它将被所有视图扩展。比如说

class GeneralParent:
   def __init__(self):
       #SETTING Up different variables 
       self.LoggedIn = false
   def isLoggedIn(self):
       return self.LoggedIn

class FirstView(TemplateView):
   ####other stuff##
   def get_context_data(self, **kwargs):
        context = super(IndexView, self).get_context_data(**kwargs)
        allLeads = len(self.getAllLeads())
        context['isLoggedIn'] = ####CALL GENEREAL PARENT CLASS METHOD###

        return context

class SecondView(FormView):
####other stuff##
   def get_context_data(self, **kwargs):
        context = super(IndexView, self).get_context_data(**kwargs)
        allLeads = len(self.getAllLeads())
        context['isLoggedIn'] = ####CALL GENEREAL PARENT CLASS METHOD###

        return context

这在django中可能吗?

继承的顺序很重要,超级级联的调用沿着继承线向下。您必须考虑在
\uuuu init\uuuu
方法的继承中可能传递的任何变量

第一个继承方法将首先被调用,第二个作为第一个父级的
\uuuuu init\uuuu
方法调用super(以便调用第二个父级的
\uuuu init\uuu
GeneralParent
必须继承自
对象
或继承自
对象
的类

class GeneralParent(object):
   def __init__(self,*args,**kwargs):
       #SETTING Up different variables 
       super(GeneralParent,self).__init__(*args,**kwargs)
       self.LoggedIn = false
   def isLoggedIn(self):
       return self.LoggedIn

class FirstView(GeneralParent,TemplateView):
   ####other stuff##
   def get_context_data(self, **kwargs):
        context = super(FirstView, self).get_context_data(**kwargs)
        allLeads = len(self.getAllLeads())
        context['isLoggedIn'] = ####CALL GENEREAL PARENT CLASS METHOD###

        return context

class SecondView(GeneralParent,FormView):
####other stuff##
   def get_context_data(self, **kwargs):
        context = super(SecondView, self).get_context_data(**kwargs)
        allLeads = len(self.getAllLeads())
        context['isLoggedIn'] = ####CALL GENEREAL PARENT CLASS METHOD###

        return context

继承的顺序很重要,超级级联的调用沿着继承线向下。您必须考虑在
\uuuu init\uuuu
方法的继承中可能传递的任何变量

第一个继承方法将首先被调用,第二个作为第一个父级的
\uuuuu init\uuuu
方法调用super(以便调用第二个父级的
\uuuu init\uuu
GeneralParent
必须继承自
对象
或继承自
对象
的类

class GeneralParent(object):
   def __init__(self,*args,**kwargs):
       #SETTING Up different variables 
       super(GeneralParent,self).__init__(*args,**kwargs)
       self.LoggedIn = false
   def isLoggedIn(self):
       return self.LoggedIn

class FirstView(GeneralParent,TemplateView):
   ####other stuff##
   def get_context_data(self, **kwargs):
        context = super(FirstView, self).get_context_data(**kwargs)
        allLeads = len(self.getAllLeads())
        context['isLoggedIn'] = ####CALL GENEREAL PARENT CLASS METHOD###

        return context

class SecondView(GeneralParent,FormView):
####other stuff##
   def get_context_data(self, **kwargs):
        context = super(SecondView, self).get_context_data(**kwargs)
        allLeads = len(self.getAllLeads())
        context['isLoggedIn'] = ####CALL GENEREAL PARENT CLASS METHOD###

        return context

它起作用了,但为什么我的类需要从对象继承?
super
仅适用于新样式的类(从
object
继承)-更多信息-它起作用了,但为什么我的类需要从对象继承?
super
仅适用于新样式的类(从
object
继承)-更多信息-