Django CMS插件上下文与页面上下文

时间:2015-02-18 10:24:34

标签: django django-cms

我有一个新闻页面,在news.detailpage中,我在另一个小栏目中有另外4个项目(新闻,视频等)作为名为CrossItems的模型的cms_plugin 。

enter image description here

问题是,cms_plugin的上下文可以包含news.detailpage中显示的新闻。

我担心的是,我无法访问来自cms_plugins上下文的网页上下文,否则我可以轻松过滤掉新闻。

有没有办法实现这一点,以便4个项目不包含显示的实际新闻?

1 个答案:

答案 0 :(得分:4)

这样的事情怎么样..

cms_plugins.py

如果你想要current_page:

def render(self, context, instance, placeholder):
    context = super(CrossItemsPlugin, self).render(context, instance, placeholder)
    request = context['request']
    page = request.current_page

   # your logic goes here

如果您想要当前的文章,那么您需要在views.py

中进行设置。
def get_context_data(self, **kwargs):
    context = super(NewsDetailView, self).get_context_data(**kwargs)
    setattr(self.request, 'current_article', self.object)
    return context

你可以在插件渲染方法中访问它。

def render(self, context, instance, placeholder):
    context = super(CrossItemsPlugin, self).render(context, instance, placeholder)
    request = context['request']
    article = request.current_article
相关问题