Django模板使用for循环计数器迭代查询集

Django模板使用for循环计数器迭代查询集,django,django-templates,Django,Django Templates,我有3个查询集,用于3种不同类型的文章:右倾、居中、左倾。我试图在3列中显示这些内容,结果如下: 右倾|中倾|左倾 右倾|中倾|左倾 右倾|中倾|左倾 因此,我有三个查询集,如视图中所示: right_articles = sline.article_set.filter(avgLean__lte=-0.75).order_by('-credibility')[:25] left_articles = sline.article_set.filter(avgLean__gte=0.75).ord

我有3个查询集,用于3种不同类型的文章:右倾、居中、左倾。我试图在3列中显示这些内容,结果如下:

右倾|中倾|左倾

右倾|中倾|左倾

右倾|中倾|左倾

因此,我有三个查询集,如视图中所示:

right_articles = sline.article_set.filter(avgLean__lte=-0.75).order_by('-credibility')[:25]
left_articles = sline.article_set.filter(avgLean__gte=0.75).order_by('-credibility')[:25]
center_articles = sline.article_set.filter(avgLean__gt=-0.75).filter(avgLean__lt=0.75).order_by('-credibility')[:25]
我在每一列中添加了一个计数器,作为上下文的一部分,如下所示:

return render(request, 'storylines/detail.html', {'n_articles': range(0, 24),
        'storyline': sline, 
        'reqUser':request.user, 
        'vote':ini, 'right_articles':right_articles, 
        'left_articles': left_articles, 
        'center_articles': center_articles})
<div class="row">
<div class= "col-4">
    Left articles: {{ left_articles.count }}
</div>
<div class= "col-4">
    Center articles: {{ center_articles.count }}
</div>
<div class= "col-4">
    Right articles: {{ right_articles.count }}
</div>
</div>
    {% for i in n_articles %}
   <div class="row">
        Trying {{ i }}<br>
        {% with left_articles.i as lt_article %}
            {% if lt_article %}
                {% show_card lt_article %}
            {% else %}
                <div class="col-4">No Left article {{ i }}</div>
            {% endif %}
        {% endwith %}

        {% with center_articles.i as cn_article %}
            {% if cn_article %}
                {% show_card cn_article %}
            {% else %}
                <div class="col-4">No Center article {{ i }}</div>
            {% endif %}
        {% endwith %}

        {% with right_articles.i as rt_article %}
            {% if rt_article %}
                {% show_card rt_article %}
            {% else %}
                <div class="col-4">No Right article {{ i }}</div>
            {% endif %}
        {% endwith %}
    </div>      
    {% endfor %}
然后,我转到模板并循环遍历如下内容:

return render(request, 'storylines/detail.html', {'n_articles': range(0, 24),
        'storyline': sline, 
        'reqUser':request.user, 
        'vote':ini, 'right_articles':right_articles, 
        'left_articles': left_articles, 
        'center_articles': center_articles})
<div class="row">
<div class= "col-4">
    Left articles: {{ left_articles.count }}
</div>
<div class= "col-4">
    Center articles: {{ center_articles.count }}
</div>
<div class= "col-4">
    Right articles: {{ right_articles.count }}
</div>
</div>
    {% for i in n_articles %}
   <div class="row">
        Trying {{ i }}<br>
        {% with left_articles.i as lt_article %}
            {% if lt_article %}
                {% show_card lt_article %}
            {% else %}
                <div class="col-4">No Left article {{ i }}</div>
            {% endif %}
        {% endwith %}

        {% with center_articles.i as cn_article %}
            {% if cn_article %}
                {% show_card cn_article %}
            {% else %}
                <div class="col-4">No Center article {{ i }}</div>
            {% endif %}
        {% endwith %}

        {% with right_articles.i as rt_article %}
            {% if rt_article %}
                {% show_card rt_article %}
            {% else %}
                <div class="col-4">No Right article {{ i }}</div>
            {% endif %}
        {% endwith %}
    </div>      
    {% endfor %}
有人能告诉我说我想说的话的正确方式吗


谢谢大家!

我不会提出任何改进建议,一个定制的标签过滤器可以做到这一点:

from django import template

register = template.Library()

@register.filter
def select_item(queryset,i):
    return queryset[i]
    # Be careful of IndexError: list index out of range
现在,您将可以访问该项目

{% with left_articles|select_item:i as lt_article %}
{% endwith %}