鹡鸰。如何通过树中较高的一个类别获取子页面列表

问题描述 投票:0回答:1

我英语不好。 长期以来,我一直在寻找问题的解决方案。 有这样一个类别树:

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”的相邻页面。

django-templates wagtail
1个回答
0
投票

使用

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 %}

© www.soinside.com 2019 - 2024. All rights reserved.