Wagtail 从抽象子类添加到上下文
Wagtail Add to context from abstract subclass
我正在制作一个可重复使用的 wagtail 应用程序,用于通过 TeamMember
模型管理团队成员。
我想要的是安装此应用程序,然后有一种简单的方法将团队成员添加到页面上下文中,而无需在每个页面模型上编写 get_context
函数。
在应用程序中,我添加了一个带有 get_context
函数的抽象 Django 模型:
class TeamPageExtension(models.Model):
def get_context(self, request):
context = super().get_context(request)
context['team'] = TeamMember.objects.all()
return context
class Meta:
abstract = True
在网站上..
class DummyTeamPage(Page, TeamPageExtension):
template = 'home/team_page.html'
这行不通。我可以看到在 TeamPageExtension 的 get_context
中调用 super() 是行不通的,但想不出另一种方法来做到这一点。
我想要的是将 team
变量添加到具有 TeamPageExtension 的任何页面模型 "extended" 的上下文中。
感谢任何帮助!
感谢 gasman 提供解决方案:
在应用程序中:
class TeamPageExtension():
def get_context(self, request):
context = super().get_context(request)
context['team'] = TeamMember.objects.all()
return context
在鹡鸰网站:
class AnotherExtension():
def get_context(self, request):
context = super().get_context(request)
context['alex'] = 'noob'
return context
class DummyTeamPage(TeamPageExtension, AnotherExtension, Page):
# Both 'team' and 'alex' are available in the template context
template = 'home/team_page.html'
我正在制作一个可重复使用的 wagtail 应用程序,用于通过 TeamMember
模型管理团队成员。
我想要的是安装此应用程序,然后有一种简单的方法将团队成员添加到页面上下文中,而无需在每个页面模型上编写 get_context
函数。
在应用程序中,我添加了一个带有 get_context
函数的抽象 Django 模型:
class TeamPageExtension(models.Model):
def get_context(self, request):
context = super().get_context(request)
context['team'] = TeamMember.objects.all()
return context
class Meta:
abstract = True
在网站上..
class DummyTeamPage(Page, TeamPageExtension):
template = 'home/team_page.html'
这行不通。我可以看到在 TeamPageExtension 的 get_context
中调用 super() 是行不通的,但想不出另一种方法来做到这一点。
我想要的是将 team
变量添加到具有 TeamPageExtension 的任何页面模型 "extended" 的上下文中。
感谢任何帮助!
感谢 gasman 提供解决方案:
在应用程序中:
class TeamPageExtension():
def get_context(self, request):
context = super().get_context(request)
context['team'] = TeamMember.objects.all()
return context
在鹡鸰网站:
class AnotherExtension():
def get_context(self, request):
context = super().get_context(request)
context['alex'] = 'noob'
return context
class DummyTeamPage(TeamPageExtension, AnotherExtension, Page):
# Both 'team' and 'alex' are available in the template context
template = 'home/team_page.html'