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?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…