视图:额外的上下文 [英] Wagtail Views: extra context

查看:66
本文介绍了视图:额外的上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到更新Wagtail CMS页面上下文的正确方法。

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"

我还希望该页面包含一些第三方信息。就我而言,它是论坛。最好写一些ViewMixin,例如:

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

我可以通过编写Django CBV来做到这一点,但我真的很想知道Wa本地人的方式。
谢谢!

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

推荐答案

您可以通过覆盖 get_context 页面模型上的方法:

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

这使变量 forums 在您的模板上可用。

This makes the variable forums available on your template.

这篇关于视图:额外的上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆