Jumy Elerossë
Jumy Elerossë

Reputation: 189

Calling data from parent page

I'm building a website with Wagtail and there's something that I can't manage to do.

I'm trying to do a "Project page" where I define the header image and a few other variables. What I can't manage to achieve is calling that header image or fields from children pages.

This is the code for the ProjectPage:

class ProjectPage(Page):
    """Landing page for each individual project"""

    image = models.ForeignKey(
        'wagtailimages.Image', null=True, blank=True,
        on_delete=models.SET_NULL, related_name='+'
    )

    intro = models.CharField(max_length=50, default='')
    about = RichTextField(blank=True)

    content_panels = Page.content_panels + [
        FieldPanel('image'),
        FieldPanel('intro'),
        FieldPanel('about')
    ]

    def get_context(self, request, *args, **kwargs):
        context = super(ProjectPage, self).get_context(request, *args, **kwargs)
        return context

How can I achieve that?

Any help is much appreciated.

Upvotes: 1

Views: 731

Answers (1)

Jumy Elerossë
Jumy Elerossë

Reputation: 189

OK, so the answer is easier than I expected.

For example, to show the intro field of the parent page in the template we just need to add this:

{{page.get_parent.specific.intro }}

In the case it needs to be in the model:

class ChildPage(Page):

    def get_context(self, request, *args, **kwargs):
        context = super().get_context(request, *args, **kwargs)
        context['hero_image'] = self.get_parent().specific.image
        return context 

Upvotes: 1

Related Questions