因为我需要循环浏览一个模型表,以在导航栏中呈现选项/导航链接,并且导航栏在每个页面上呈现。因此,我发现自己在相同的上下文中传递给每个基于类的视图,并且这是非常重复的:
class Xxx:
...
def get_context_data(self, *args, **kwargs):
context = super().get_context_data(*args, **kwargs)
context['categories'] = Category.objects.get_categories_with_item()
return context
是否有采用DRYer的方法?谢谢!!
PS。 而且*args
方法中是否需要get_context_data
?我看过带有它的代码,而没有
因为我需要循环浏览一个模型表,以在导航栏中呈现选项/导航链接,并且导航栏在每个页面上呈现。因此,我发现自己在相同的上下文中传递给每个类-...
您可以编写自定义context processor