Python 将两个queryset组合成一个模板标记

Python 将两个queryset组合成一个模板标记,python,django,django-templates,Python,Django,Django Templates,我有两种模型news.article和portfolio.entry。两种模型都有一个布尔字段,用于将“is_campaign”设置为true 我正试图写一个自定义模板标签,这样我就可以得到最新的活动文章(应该只有一篇) 这是我的模板标签:campaign_article.py from itertools import chain from django import template from news.models import Article from portfolio.models

我有两种模型news.article和portfolio.entry。两种模型都有一个布尔字段,用于将“is_campaign”设置为true

我正试图写一个自定义模板标签,这样我就可以得到最新的活动文章(应该只有一篇)

这是我的模板标签:campaign_article.py

from itertools import chain
from django import template

from news.models import Article
from portfolio.models import Entry

register = template.Library()

def get_campaign():
        #Get the newest news article with is_campaign=True
        article = Article.objects.filter(is_campaign=True).order_by('-pub_date')[:1]

        #Get the newest portfolio entry with is_campaign=True
        portfolio = Portfolio_entry.objects.filter(is_campaign=True).order_by('-pub_date')[:1]

        #combine article, and entry and display only the newest
        campaign_article = list(chain(article, portfolio))[:1]


        return {'campaign_article': campaign_article}



register.tag('campaign', get_campaign)
我已在我的模板中尝试过:

{% load campaign_article %}
{% for campaign_article in campaign %}

{{ campaign_article.id }}

{% endfor %}

但我没有得到任何输出。这是错误的方法吗?

您不需要创建模板标记来完成所需的操作。阅读以下内容:

在你看来:

def some_view(request):
    # ...
    c = RequestContext(request, {
        'foo': 'bar',
    }, [get_campaign]) # attach your context processor to template context
    return HttpResponse(t.render(c))
UPD:如果需要在每个页面上显示数据,可以在设置文件中将上下文处理器注册为全局。请参阅设置

TEMPLATE_CONTEXT_PROCESSORS = (..., "myapp.context_processors.get_campaign")

Django将自动向每个模板呈现添加变量
campaign\u article

您无需创建模板标记即可完成所需操作。阅读以下内容:

在你看来:

def some_view(request):
    # ...
    c = RequestContext(request, {
        'foo': 'bar',
    }, [get_campaign]) # attach your context processor to template context
    return HttpResponse(t.render(c))
UPD:如果需要在每个页面上显示数据,可以在设置文件中将上下文处理器注册为全局。请参阅设置

TEMPLATE_CONTEXT_PROCESSORS = (..., "myapp.context_processors.get_campaign")
Django将自动向每个模板呈现添加变量
campaign\u article

您希望创建的是一个通用标记,而不是一个通用标记。 因此,您可以将标记更新为:

def get_campaign():
    #your stuff
    ....

    return campaign_article

register.assignment_tag(get_campaign, name='campaign')
并将模板更新为:

{% load campaign_article %}
{% campaign as campaign_list %} {# loads the tags and creates campaign_list context variable #}
{% for campaign_article in campaign_list %}
    {{ campaign_article.id }}
{% endfor %}
您可能希望创建一个通用标记,而不是一个通用标记。 因此,您可以将标记更新为:

def get_campaign():
    #your stuff
    ....

    return campaign_article

register.assignment_tag(get_campaign, name='campaign')
并将模板更新为:

{% load campaign_article %}
{% campaign as campaign_list %} {# loads the tags and creates campaign_list context variable #}
{% for campaign_article in campaign_list %}
    {{ campaign_article.id }}
{% endfor %}

如果我想在页脚的每一页上都开展此活动,不确定这是否是最好的方式?如果我想在页脚的每一页上开展此活动,不确定这是否是最好的方式?