SHq88
SHq88

Reputation: 37

Getting the context of a specific page in Wagtail / Puput

I have a Django/Wagtail/Puput site with this structure:

RootPage
  |
  |- BlogPage (Puput)
  |- InformationPage

I'm trying to display summary info from the Puput blog on the InformationPage. This works with this code, as long as I only have one BlogPage:

class InformationPage(Page):
    body = RichTextField(verbose_name=_("body"))

    . . . 

    def get_context(self, request, *args, **kwargs):
        context = super(InformationPage, self).get_context(
            request, *args, **kwargs)
        context['blog_page'] = BlogPage.objects.first()
        context['information_page'] = self
        return context

But I'm trying to make it work with more than one blog page. It seems like this should work:

class InformationPage(Page):
    body = RichTextField(verbose_name=_("body"))
    blog_page = models.ForeignKey('wagtailcore.Page', on_delete=models.PROTECT, related_name="information_blog")

    content_panels = [
        MultiFieldPanel(
            [
                FieldPanel("title", classname="title"),
                FieldPanel("body", classname="full"),
                PageChooserPanel('blog_page'),
            ],
            heading=_("Content"),
        )]

    def get_context(self, request, *args, **kwargs):
        context = super(InformationPage, self).get_context(
            request, *args, **kwargs)
        context['blog_page'] = self.blog_page
        context['information_page'] = self
        return context

But it doesn't. This was suggested by @gasman here. In other words, if I refer to the blog page properties using context['blog_page'] = BlogPage.objects.first(), everything works fine, but switching it out to use context['blog_page'] = self.blog_page (and selecting the correct blog page in the admin) does not work.

Without switching it out, I think I can only ever have a single instance of BlogPage, because all InformationPages will have to pull from the first instance.

Any thoughts?

Upvotes: 1

Views: 811

Answers (1)

gasman
gasman

Reputation: 25292

You haven't given a description of the problem beyond "it doesn't work", so I'm only guessing here, but you're presumably trying to output fields of the blog page that are part of the BlogPage model. This doesn't work because blog_page is defined as a foreign key to wagtailcore.Page, and so accessing self.blog_page only gives you a basic Page object consisting of the core fields such as title. You can retrieve the full BlogPage object by accessing self.blog_page.specific:

context['blog_page'] = self.blog_page.specific

However, a smarter approach is to change your foreign key to point to BlogPage, since presumably choosing any other page type is not valid here:

blog_page = models.ForeignKey('my_blog_app.BlogPage', on_delete=models.PROTECT, related_name="information_blog")

With this change, self.blog_page will return a BlogPage instance directly, and there's no need for .specific.

Upvotes: 1

Related Questions