gpt4 book ai didi

python - Wagtail:在父页面内显示子页面列表

转载 作者:太空狗 更新时间:2023-10-29 20:35:52 30 4
gpt4 key购买 nike

在 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(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'),
]

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

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

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

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

最佳答案

来自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.

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

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

至少从 0.8 版开始,以下内容也应该可以使用 specific:

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

关于python - Wagtail:在父页面内显示子页面列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32429113/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com