python Django.access在子for循环中的列表索引

3npbholx  于 2023-01-08  发布在  Python
关注(0)|答案(2)|浏览(104)

如何根据外部循环的计数器来改变内部for循环列表的访问索引?在普通的python中,我会这样做

parent_list = ['One','Two','Three']
child_list = ['A','B','C']

for idx, item in enumerate(parent_list):
    for child_item in child_list[idx]:
        print(str(idx), item, child_item)

我一直在考虑使用with和forloop.counters,但是我遇到了索引没有被访问或者索引没有改变的情况,这就是我目前的情况。

{% for item in payout_items %}
        {% with forloop.counter0 as outer_counter %}
        <h2>{{ item.market_place }} on {{ item.entry_date }}:
            {% for item in royalty_items.outer_counter %}
                <tr>
                <th scope="row">{{ item.id }}</th>
                <td>{{ item.entry_date }}</td>
                <td>{{ item.market_place }}</td>
                </tr>
            {% endfor %}
        {% endwith %}
    {% endfor %}

如果我改变

{% for item in royalty_items.outer_counter %}

{% for item in royalty_items.0 %}

第一个索引重复了很多次,我可以看到outer_counter从输出中递增,但只需要royalty_items来递增访问的索引。
根据要求,视图为

detail_object = AccountingPeriod.objects.get(pk=detail_id)
    payout_items = Payout.objects.filter(sales_period_start__range=[detail_object.period_start, detail_object.period_end])

    royalty_items = []
    for payout in payout_items:
        temp = Royalty.objects.filter(entry_date__range=[payout.sales_period_start, payout.sales_period_end]).filter(market_place=payout.market_place)
        print(str(payout.sales_period_start) + " - " + str(payout.sales_period_end) + " - " + payout.market_place + " = " + str(len(temp)))
        royalty_items.append(temp)

并传递以下呈现调用。

render(request, 'royalties/accounting_period/summary.html', {'detail_object': detail_object, 'payout_items': payout_items, 'royalty_items': royalty_items})

解决方案:我创建了一个模板过滤器,但觉得应该有一个更优雅的答案。

@register.filter()
def getRoyaltySet(royalty_items, outer_counter):
    return royalty_items[outer_counter]
cig3rfwq

cig3rfwq1#

我只想把这个加到项目中,所以:

detail_object = get_object_or_404(AccountingPeriod, pk=detail_id)
payout_items = Payout.objects.filter(
    sales_period_start__range=(
        detail_object.period_start,
        detail_object.period_end,
    )
)
for payout in payout_items:
    temp = Royalty.objects.filter(
        entry_date__range=(payout.sales_period_start, payout.sales_period_end),
        market_place=payout.market_place,
    )
    print(
        f'{payout.sales_period_start} - {payout.sales_period_end} - {payout.market_place} = {len(temp)}'
    )
    payout.royalities = temp

然后您可以通过.royalities访问该文件:

{% for item in payout_items %}
    <h2>{{ item.market_place }} on {{ item.entry_date }}</h2>:
        {% for subitem in item**.royalites** %}
            <!-- … -->
        {% endfor %}
{% endfor %}
mklgxw1f

mklgxw1f2#

您可以使用forloop.parentloop来访问外部forloop

{{ forloop.parentloop.counter }}

如果有嵌套循环,您甚至可以将它们链接起来:

{{ forloop.parentloop.parentloop.counter }}

如果你想完全使用自己的索引,你可以使用.items来访问字典:

{% for key, value in mydict.items %}
    {% for key2, value2 in otherdict.items %}
        <div>{{ key }} - {{ key2 }}</div>
    {% endfor %}
{% endfor %}

相关问题