0
votes

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.

1
I have tried the solutions in that question - I can't get any attributes other than title.geonaut
I'd expect changing self.get_children() to self.get_children().specific() to solve this - are you sure this doesn't work?gasman
I realised that I could add the specific() method to the model as well as in the template, as alluded to in the other question. Adding it to the model worked. I'll write it up here anyway, as this is a slightly different slant on the other answer. Thanks @gasmangeonaut

1 Answers

1
votes

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.