I have two models news.article and portfolio.entry. Both models have a BooleanField for setting “is_campaign” to true.
Im trying to write a custom templatetag so I can get the latest campaign article (There should only be one)
Here is my templatetag: 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)
I have tried this in my template:
{% load campaign_article %}
{% for campaign_article in campaign %}
{{ campaign_article.id }}
{% endfor %}
But I don’t get any output. Is this the wrong method ?
You would want to create
assignment_tagrather than a generic tag.So you can update your tag as:
And update template as: