在 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 %}
这将显示所有子页面标题,但不显示图像。是否可以检索子页面的图像字段?