Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wagtail Views: extra context

I don't found proper way to update Wagtail CMS Page context.

For instance i have my homepage model:

class HomePage(Page):
    about = RichTextField(blank=True)
    date = models.DateField(auto_now=True)

    content_panels = Page.content_panels + [
        FieldPanel('about', classname="full")
    ]

    class Meta:
        verbose_name = "Homepage"

And I also want some third part information to be included on that page. In my case its forum. It will be great to write some ViewMixin, like:

class ForumMixin(object):
    pass
    # add latest forums to context

I can do it by writing my Django CBV, but i really want to know Wagtail Native Way. Thanks!

like image 983
yanik Avatar asked Sep 17 '15 09:09

yanik


1 Answers

You can do this by overriding the get_context method on your page model:

class HomePage(Page):
    def get_context(self, request):
        context = super(HomePage, self).get_context(request)
        context['forums'] = Forum.objects.all()
        return context

This makes the variable forums available on your template.

like image 58
gasman Avatar answered Sep 22 '22 16:09

gasman