我有两个模型news.article 和portfolio.entry。两个模型都有一个用于将“is_campaign”设置为 true 的 BooleanField。
我正在尝试编写自定义模板标签,以便获取最新的广告系列文章(应该只有一篇)
这是我的模板标签: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 %}
但我没有得到任何输出。这是错误的方法吗?