Wagtail:显示父页面中的子页面列表 [英] Wagtail: Display a list of child pages inside a parent page

查看:1130
本文介绍了Wagtail:显示父页面中的子页面列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Wagtail CMS中,我正在尝试创建一个索引页面,该页面将显示其所有子页面的列表以及与每个子页面相关联的特征图像。



<我已经在models.py中创建了这两个页面模型:

  class IndexPage(Page):
intro = RichTextField(blank = True)

content_panels = Page.content_panels + [
FieldPanel('intro',classname ='full'),
]

subpage_types = ['myapp.ItemPage']


class ItemPage(页):
representative_image = models.ForeignKey(
'wagtailimages.Image',
null = True,
blank = True,
on_delete = models.SET_NULL,
related_name ='+'


body = RichTextField blank = True)

promotion_panels = Page.promote_panels + [
ImageChooserPanel('representative_image'),
]

content_panels = Page.content_panels + [
FieldPanel('body',classname ='full'),
]

在模板index_page.html中,我添加了以下代码:

 < div class =intro> { {self.intro | richtext}}< / div> 

{%for self.get_children%}
{{page.title}}
{%image page.representative_image width-400%}
{% endfor%}

这将显示所有子页面标题,但不显示图像。是否可以检索子页面的图像字段?

解决方案

wagtail版本1.1的发行说明



< blockquote>

通常,检索页面的查询(例如 homepage.get_children())的操作将返回它们作为基本页面实例,只包括核心页数据,如标题。 specific()方法(例如 homepage.get_children()。specific())现在允许它们被检索为他们最具体的类型,使用最少的查询。


因此,您不需要在即将发布的版本1.1中自定义功能您可以将模板更改为:

  {%for self.get_children.specific%} 
{ page.title}}
{%image page.representative_image width-400%}
{%endfor%}

至少自0.8版以来,以下内容也可以使用具体

  {%for self.get_children%} 
{{page.title}}
{%image page.specific.representative_image width-400%}
{%endfor%}


In Wagtail CMS, I'm trying to create an index page that will display a list of all its child pages along with a featured image associated with each child page.

I have created these two page models in models.py:

class IndexPage(Page):
    intro = RichTextField(blank=True)

    content_panels = Page.content_panels + [
        FieldPanel('intro', classname='full'),
    ]

    subpage_types = ['myapp.ItemPage']


class ItemPage(Page):
    representative_image = models.ForeignKey(
        'wagtailimages.Image',
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name='+'
    )

    body = RichTextField(blank=True)

    promote_panels = Page.promote_panels + [
        ImageChooserPanel('representative_image'),
    ]

    content_panels = Page.content_panels + [
        FieldPanel('body', classname='full'),
    ]

In the template index_page.html, I added the following code:

<div class="intro">{{ self.intro|richtext }}</div>

{% for page in self.get_children %}
  {{ page.title }}
  {% image page.representative_image width-400 %}
{% endfor %}

This displays all of the child page titles, but not the images. Is it possible to retrieve the image fields for the child pages?

解决方案

From the release notes of wagtail version 1.1:

Usually, an operation that retrieves a queryset of pages (such as homepage.get_children()) will return them as basic Page instances, which only include the core page data such as title. The specific() method (e.g. homepage.get_children().specific()) now allows them to be retrieved as their most specific type, using the minimum number of queries.

Therefore you don't need your custom function in the upcoming release 1.1 anymore and you can change your template to:

{% for page in self.get_children.specific %}
    {{ page.title }}
    {% image page.representative_image width-400 %}
{% endfor %}

At least since version 0.8 the following should work, too, using specific:

{% for page in self.get_children %}
    {{ page.title }}
    {% image page.specific.representative_image width-400 %}
{% endfor %}

这篇关于Wagtail:显示父页面中的子页面列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆