Django模板:在不同的列表上组合2个循环

问题描述 投票:0回答:1

我想知道如何在我的模板中组合这两个查询集以循环它们。

requests = Download.objects.values('pub__age_id').annotate(count=Count('pub__age_id'))

max_download_number = Download.objects.values('pub__age_id').annotate(max_usage=Max('usage'))

context = {'requests': requests, 'max_download_number': max_download_number}

在我的模板中:

{% for item in requests %}
    {% for element in max_download_number %}
        <tr>
            <td>{{ item.pub__age_id }}</td>
            <td><span class="badge alert-info">{{ item.count }}</span></td>
             <td>{{ element.max_usage }}</td>
             <td>Something</td>
        </tr>
    {% endfor %}
{% endfor %}

它显示错误的循环:

enter image description here

django django-templates
1个回答
4
投票

为什么不在视图中组合它:

requests = Download.objects.values('pub__age_id').annotate(count=Count('pub__age_id')).annotate(max_usage=Max('usage'))

然后在模板中:

<td>{{ item.pub__age_id }}</td>
<td><span class="badge alert-info">{{ item.count }}</span></td>
<td>{{ item.max_usage }}</td>
© www.soinside.com 2019 - 2024. All rights reserved.