我英语不好。 长期以来,我一直在寻找问题的解决方案。 有这样一个类别树:
class ProductPage(Page):
parent_page_types = ['CatalogSubPage']
pass
class ProductSubPage(Page):
parent_page_types = ['ProductPage']
pass
class ProductSubSubPage(Page):
parent_page_types = ['ProductSubPage']
pass
我找不到如何在“ProductSubSubPage”页面上显示“ProductPage”的子页面或“ProductSubPage”的相邻页面。
get_parent
检索父页面,然后使用 get_siblings
获取其相邻页面。
class ProductSubSubPage(Page):
parent_page_types = ['ProductSubPage']
def get_context(self, request, *args, **kwargs):
context = super().get_context(request, *args, **kwargs)
context['adjacent_pages'] = self.get_parent().get_siblings().live().specific()
return context
然后您可以在模板中访问
adjacent_pages
,例如{% for page in adjacent_pages %}
。