2014-11-24 55 views
-1

我有一個頁面,可以使用插件和apphooks。django-cms - 如何知道哪些apphooks是活動的

它就像一個頁面,可以是新聞頁面,星座頁面和圖片頁面,這些頁面都是帶插件的app(hook)。

在新聞應用程序cms_plugins.py我想呈現像2 ..新聞,2星座和2圖片..這意味着我需要確保這3個應用程序真正在這個頁面中激活混合項目。

這是我的新聞插件的render部分:

def render(self, context, instance, placeholder): 
    news = MyNews.objects.order_by('-id')[:2] 
    #horoscopes = Astro.objects.order_by('-id')[:2] 
    #pics = Pics.objects.order_by('-id')[:2] 
    context.update({ 
     "instance": instance, 
     "news": news, 
     "placeholder": placeholder 
    }) 
    return context 

我怎樣才能知道活動apphooks?例如我怎麼能知道星座 - 頁面和圖片頁面是否存在,以便我可以渲染它們?

回答

0

我發現CMS的Page模式,解決這個問題是這樣的:

from cms.models import Page 

def render(self, context, instance, placeholder): 
    news = MyNews.objects.order_by('-id')[:2] 

    lang = context['request'].LANGUAGE_CODE 
    #check if horoscope in current context language is activated for the current domain 
    if Page.objects.filter(application_namespace__iexact='astro', languages__contains=lang).exists(): 
     horoscopes = Astro.objects.order_by('-updated')[:2] 

    context.update({ 
     "instance": instance, 
     "news": news, 
     "placeholder": placeholder 
    }) 
    return context 
相關問題