Wagtail Views: extra context

我与影子孤独终老i 提交于 2019-11-30 05:45:54

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/32626815/wagtail-views-extra-context

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!