将两个查询集合到一个模板标签中 [英] Combining two queryset into a template tag

查看:102
本文介绍了将两个查询集合到一个模板标签中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个模型news.article和portfolio.entry。两个模型都有一个BooleanField,用于将is_campaign设置为true。



我试图编写一个自定义模板,以便我可以获得最新的广告系列文章(应该只有一个)



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

  from itertools import chain 
from django import template

from news.models import文章
from portfolio.models import Entry

register = template.Library()

def get_campaign():
#通过is_campaign = True
获取最新的新闻文章article = Article.objects.filter(is_campaign = True).order_by(' - pub_date')[ :1]

#获取最新的投资组合条目,其中is_campaign = True
portfolio = Portfolio_entry.objects.filter(is_campaign = True).order_by(' - pub_date')[:1]

#combine文章,并输入并显示最新的
campaign_article = list(chain(article,portfolio))[:1]


返回{'campaign_article':campaign_article}



register.tag('campaign',get_campaign)

我在我的模板中尝试过:

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

{{campaign_article.id}}

{%endfor%}

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

解决方案

您将要创建 assignment_tag ,而不是通用标签。
所以你可以更新您的标签:

  def get_campaign():
#你的东西
....

return campaign_article

register.assignment_tag(get_campaign,name ='campaign')

并更新模板为:

  {%load campaign_article%} 
{%campaign as campaign_list%} {#加载标签并创建campaign_list上下文变量#}
{%for campaign_article in campaign_list%}
{{campaign_article.id}}
{%endfor%}


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_tag rather than a generic tag. So you can update your tag as:

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

    return campaign_article

register.assignment_tag(get_campaign, name='campaign')

And update template as:

{% 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 %}

这篇关于将两个查询集合到一个模板标签中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆