Django:在基于 class 的视图中添加另一个子 class
Django: Add another subclass in a class based view
这是我的第一个 Django 应用程序,我想知道是否可以有一个通用的 class,它将被所有视图扩展。例如
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 中可行吗?
继承顺序很重要,超级级联的调用沿继承线向下。您必须考虑可能在 __init__
方法中继承的任何变量。
第一个继承方法将首先被调用,第二个作为第一个父类的 __init__
方法调用 super(为了调用第二个父类的 __init__
)。 GeneralParent
必须继承自 object
或继承自 object
的 class。
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
这是我的第一个 Django 应用程序,我想知道是否可以有一个通用的 class,它将被所有视图扩展。例如
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 中可行吗?
继承顺序很重要,超级级联的调用沿继承线向下。您必须考虑可能在 __init__
方法中继承的任何变量。
第一个继承方法将首先被调用,第二个作为第一个父类的 __init__
方法调用 super(为了调用第二个父类的 __init__
)。 GeneralParent
必须继承自 object
或继承自 object
的 class。
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