嵌套循环中的 Django 模板计数器 [英] Django template counter in nested loops
问题描述
我有两个字典的列表,我要传递给 Django 模板:
Hi I have a list of two dictionaries I am passing to a Django template:
base_parts = [
{'important item': 43},
{'lesser item': 22, 'lesser item': 3, 'lesser item': 45}
]
在我的模板中,我可以这样做:
in my template I can do this:
{% for base_part in base_parts %}
{% for k, v in base_part.items %}
{# ...do stuff #}
{# I try to get a running total of items to use as an ID #}
inner ID: {% forloop.counter0 %}< br/>
outer ID: {% forloop.parentloop.counter0 %}< br/>
{% endfor %}
{% endfor %}
如您所见,我想要的是我迭代过的项目总数的运行总和,但是我包含的两种方法都返回重复项.我知道我可以连接循环,但我使用的是表单集并且真的希望将 id 索引为 0、1、2...等.
As you can see, what I want is a running total of the total number of items I have iterated through, but both methods I have included return duplicates. I know I could concatenate the loops, but I am using a formset and really would like the ids to be indexed 0,1,2...etc.
有没有办法在模板中实现这种类型的计数?
Is there a way to achieve this type of count in the template?
非常感谢任何帮助.
编辑
此刻的输出看起来像:
outerID: 0<br />
innerID: 0<br />
outerID: 0<br />
innerID: 1<br />
outerID: 1<br />
innerID: 0<br />
outerID: 1<br />
innerID: 1<br />
outerID: 1<br />
innerID: 2<br />
我想要:
totalID: 0<br />
totalID: 1<br />
totalID: 2<br />
totalID: 3<br />
totalID: 4<br />
totalID: 5<br />
totalID: 6<br />
totalID: 7<br />
totalID: 8<br />
totalID: 9<br />
推荐答案
我用 itertools
找到了更好的解决方案.(比我之前的答案更好)您可以将循环的当前状态设置为发送到视图上下文的 itertools 变量.这次我尝试了一个虚拟的 Django 项目,它的效果非常好.
I found a better solution with itertools
. (Better than my previous answer)
You can set current state of the loop to the itertools variable sent to the view context.
This time i tried on a dummy Django project and it works like a charm.
views.py:
from django.shortcuts import render_to_response
import itertools
def home(request):
iterator=itertools.count()
base_parts = [
{'important item': 43},
{'lesser item1': 22, 'lesser item2': 3, 'lesser item3': 45},
{'most important item': 55}
]
return render_to_response('index.html',
{'base_parts': base_parts, 'iterator':iterator})
index.html:
index.html:
{% for base_part in base_parts %}
{% for k, v in base_part.items %}
{{ iterator.next }} - {{ v }}<br/>
{% endfor %}
{% endfor %}
HTML 输出:
0 - 43
1 - 22
2 - 45
3 - 3
4 - 55
<小时>
排序值:
(这部分不是对实际问题的回答.更像是我在玩)
(This part is not an answer to the actual question. It's more like I'm playing around)
您可以使用 Django 的 SortedDict 而不是 Python 的内置字典来保持项目顺序.
You can use Django's SortedDict instead of Python's built-in dictionary to keep items order.
views.py
from django.shortcuts import render_to_response
import itertools
from django.utils.datastructures import SortedDict
def home(request):
iterator=itertools.count()
base_parts = [
SortedDict([('important item', 43)]),
SortedDict([('lesser item1', 22),
('lesser item2', 3),
('lesser item3', 45)]),
SortedDict([('most important item', 55)])
]
print base_parts[1]
return render_to_response('index.html',
{'base_parts': base_parts, 'iterator':iterator})
HTML 输出:
0 - 43
1 - 22
2 - 3
3 - 45
4 - 55
<小时>
编辑 2014 年 5 月 25 日
您也可以使用 collections.OrderedDict 而不是Django 的 SortedDict.
You can also use collections.OrderedDict instead of Django's SortedDict.
编辑 2016 年 6 月 28 日
调用 iterator.next
在 Python 3 中不起作用.您可以创建自己的迭代器类,继承自 itertools.count:
Calling iterator.next
doesn't work in Python 3. You can create your own iterator class, inheriting from itertools.count:
import itertools
class TemplateIterator(itertools.count):
def next(self):
return next(self)
这篇关于嵌套循环中的 Django 模板计数器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!