Python 如何显示摇摆页的子项

Python 如何显示摇摆页的子项,python,django,wagtail,Python,Django,Wagtail,我的要求听起来很简单,但我正在努力让它工作 在我的wagtail项目中,我有一个BlogListingPage,它有许多子页面。我试图在网页中创建一个孩子列表。这是我的密码: 型号.py class BlogListingPage(Page): ... def get_context(self, request, *args, **kwargs): context = super().get_context(request, *args, **kwargs

我的要求听起来很简单,但我正在努力让它工作

在我的wagtail项目中,我有一个BlogListingPage,它有许多子页面。我试图在网页中创建一个孩子列表。这是我的密码:

型号.py

class BlogListingPage(Page):    
    ...
    def get_context(self, request, *args, **kwargs):
        context = super().get_context(request, *args, **kwargs)
        context['blog_pages'] = BlogDetailPage.objects.live().child_of(self)
        return context


class BlogDetailPage(Page):
    ...    
    blog_listing = models.ForeignKey(BlogListingPage,
                                     on_delete=models.PROTECT,
                                     blank=True,
                                     null=True,)
def blog(request):
    url = 'blog/blog_listing_page.html'
    context = {'data': BlogListingPage.objects.all()}
    return render(request, url, context)
视图.py

class BlogListingPage(Page):    
    ...
    def get_context(self, request, *args, **kwargs):
        context = super().get_context(request, *args, **kwargs)
        context['blog_pages'] = BlogDetailPage.objects.live().child_of(self)
        return context


class BlogDetailPage(Page):
    ...    
    blog_listing = models.ForeignKey(BlogListingPage,
                                     on_delete=models.PROTECT,
                                     blank=True,
                                     null=True,)
def blog(request):
    url = 'blog/blog_listing_page.html'
    context = {'data': BlogListingPage.objects.all()}
    return render(request, url, context)
blog\u listing\u page.html

{% block content %}
    {% for blog_listing in data %}
        {% for post in blog_listing.blogdetailpage %}
            <a href="{% pageurl post %}">{{ post.blog_title }}</a>
        {% endfor %}
    {% endfor %}
{% endblock content %}
{%block content%}
{%用于数据%中的blog_列表}
{blog_listing.blogdetailpage%}中的帖子占%
{%endfor%}
{%endfor%}
{%endblock内容%}
我迷失在django/wagtail页面/objects/querysets/all()/specific的迷雾中


有人能带我去碧波荡漾吗?

关于你的方法有几点建议:

  • blogdailpage
    BlogListingPage
    使用外键不是组织父子关系的摇尾方式。所有
    Page
    对象都已经有了树层次结构,您应该依赖它,否则您将丢失Wagtail提供的所有内置树处理

    您应该使用指定页面类型之间的关联方式。在您的情况下,它看起来像:

    class BlogListingPage(Page):    
        subpage_types = ['myapp.BlogDetailPage']
    
    class BlogDetailPage(Page):
        parent_page_types = ['myapp.BlogListingPage']
    
  • BlogListingPage
    上的
    get\u context()
    方法仅在Wagtail为视图中的特定页面提供服务时运行。当您在
    博客
    视图中迭代这些页面时,它不会运行。为了访问那里的孩子,您需要执行以下操作:

    {% for blog_listing in data %}
        {% for post in blog_listing.get_children.live %}
            <a href="{% pageurl post %}">{{ post.blog_title }}</a>
        {% endfor %}
    {% endfor %}
    
    {%for blog_在数据%中列出}
    {blog_listing.get_children.live%}
    {%endfor%}
    {%endfor%}
    
    i、 例如,您需要使用页面上的
    get_children()
    live()
    进一步筛选此项以排除未发布的页面。注意,只有当博客文章是列表页面的子项(在Wagtail页面树的意义上)时,这才有效
    specific()
    在这里并不重要,如果您只使用post URL和标题-如果您想显示作为模型一部分的字段,而不是基本
    页面
    模型,则这一点很重要


  • 这回答了你的问题吗@心灵术士:“我迷失在django/wagtail页面/对象/查询集/all()/specific的迷雾中”(+“视图/标记/模板”)。感谢你的提问,你得到的答案让这里的雾少了一点。我在对答案的评论中发布了一个后续澄清请求。谢谢。救生员。非常好的解决方案描述关于调用
    get_children()
    或其等效项的位置,是否有性能方面的考虑?答案是通过模板中的标记显示迭代,但您可以替代页面模型中的上下文,'context['blog\u entries']=BlogPage.objects.child\u of(self)
    返回上下文'
    然后在模板中迭代“blog\u entries”。两种方法都更快/更好吗?