Django 如何在许多基于类的视图中使用相同的上下文变量

Django 如何在许多基于类的视图中使用相同的上下文变量,django,django-views,Django,Django Views,我希望在许多基于类的视图中使用相同的上下文变量,并利用我对Python的基本知识,通过创建一个超类和使用多重继承实现了这一点: class ContextCommonToManyViews(): def addToContext(self, context): context['form'] = FormForManyPages() context['username'] = self.request.user.username cont

我希望在许多基于类的视图中使用相同的上下文变量,并利用我对Python的基本知识,通过创建一个超类和使用多重继承实现了这一点:

class ContextCommonToManyViews():
    def addToContext(self, context): 
        context['form'] = FormForManyPages()
        context['username'] = self.request.user.username
        context['available_in_many_views'] = something
        return context

class ViewA(ListView, ContextCommonToManyViews):
    model = ModelA

    def get_context_data(self, **kwargs):
        context = super(ViewA, self).get_context_data(**kwargs)
        context = self.addToContext(context)
        context['specific_to_view'] = 'ViewA here'
        return context

class ViewB(ListView, ContextCommonToManyViews):
    model = ModelB

    def get_context_data(self, **kwargs):
        context = super(ViewB, self).get_context_data(**kwargs)
        context = self.addToContext(context)
        context['specific_to_view'] = 'ViewB here'
        return context

有更好的方法吗?

像这样的混音可能更干净:

class ContextCommonToManyViews(object):
    def get_context_data(self, request, **kwargs):
        context = super(ContextCommonToManyViews, self).get_context_data(request, **kwargs)
        context['form'] = FormForManyPages()
        context['username'] = self.request.user.username
        context['available_in_many_views'] = something
        return context

class ViewA(ContextCommonToManyViews, ListView):
    model = ModelA

class ViewB(ContextCommonToManyViews, ListView):
    model = ModelB

    def get_context_data(self, request, **kwargs):
        context = super(ViewB, self).get_context_data(request, **kwargs)
        context.update({'specific_to_B': 'some_value'})
        return context

这样的混音可能更干净:

class ContextCommonToManyViews(object):
    def get_context_data(self, request, **kwargs):
        context = super(ContextCommonToManyViews, self).get_context_data(request, **kwargs)
        context['form'] = FormForManyPages()
        context['username'] = self.request.user.username
        context['available_in_many_views'] = something
        return context

class ViewA(ContextCommonToManyViews, ListView):
    model = ModelA

class ViewB(ContextCommonToManyViews, ListView):
    model = ModelB

    def get_context_data(self, request, **kwargs):
        context = super(ViewB, self).get_context_data(request, **kwargs)
        context.update({'specific_to_B': 'some_value'})
        return context

我觉得一切都很好。您可以使用
context.update(self.addToContext())
而不是
context=self.addToContext(context)
。我觉得一切都很好。您可以使用
context.update(self.addToContext())
而不是
context=self.addToContext(context)
。它看起来确实更干净。有没有办法做到
上下文['specific_to_view']='ViewB here'
?抱歉,应该在问题中更清楚地说明这一要求。在ViewB define get_context_data()中,使用super调用它,并使用“specific_to_view”值进行更新,正如@dimablind所说的那样。我用一个例子更新了答案。它看起来确实更干净。有没有办法做到
上下文['specific_to_view']='ViewB here'
?抱歉,应该在问题中更清楚地说明这一要求。在ViewB define get_context_data()中,使用super调用它,并使用“specific_to_view”值进行更新,正如@dimablind所说的那样。我用一个例子更新了答案。