Reputation: 379
I'm trying to access the body text of a child page in my Django / Wagtail CMS blog. I can return the child page title, but I don't know how to use that to get the rest of the child page attributes. The parent is IndexPage, and the child is IndexListSubPage. My model is:
class IndexPage(Page):
body = RichTextField(blank=True)
feed_image = models.ForeignKey(
'wagtailimages.Image',
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name='+'
)
content_panels = Page.content_panels + [
FieldPanel('body', classname="full"),
ImageChooserPanel('feed_image'),
]
def get_context(self, request):
context = super(IndexPage, self).get_context(request)
context['sub_pages'] = self.get_children()
return context
class IndexListSubPage(Page):
body = RichTextField(blank=True)
feed_image = models.ForeignKey(
'wagtailimages.Image',
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name='+'
)
content_panels = Page.content_panels + [
FieldPanel('body', classname="full"),
ImageChooserPanel('feed_image'),
]
I have tried various combinations in my template:
{{ sub_pages }} //returns <QuerySet [<Page: Page title here>]>
{{ sub_pages.body }} //returns nothing
This returns the page title for the child page, but I also want other attributes, like the body text. Any ideas? I have tried the image template settings from here as well - again, I can get the title, but no attributes. The page has both image and body text in the admin interface.
Upvotes: 1
Views: 667
Reputation: 379
I got it working by changing the model to include .specific()
, as suggested by @gasman. The working model is:
class ProjectsPage(Page):
body = RichTextField(blank=True)
content_panels = Page.content_panels + [
FieldPanel('body', classname="full"),
]
def get_context(self, request):
context = super(ProjectsPage, self).get_context(request)
context['sub_pages'] = self.get_children().specific()
print(context['sub_pages'])
return context
And in the template:
{% with sub_pages as pages %}
{% for page in pages %}
{{ page.title }}
{{ page.body }}
{% endfor %}
{% endwith %}
The title and the body of the child page are now being rendered.
Upvotes: 2