英文:
How can I get index of list in django template
问题
I have 3 list item1, item2, item3
views.py
context = {'item0': region.objects.all(),
'item1': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
'item2': ['one', 'two', 'three', 'four', 'five'],
'item3': ['six', 'seven', 'eight', 'nine', 'ten'],
'item4': "test",
'item5': 5,
'item6': range(5),
}
return render(request, 'btscreate.html', context)
I tried to get in the template:
{% for num in item1 %}
<td> {{ num }} </td> # this works
<td> {{ how can I get values item2 and item3 ? }} </td>
{% endfor %}
{% for num in item6 %}
<td> {{ item2.0 }} </td> # that works
<td> {{ item2.num }} </td> # but that doesn't work
{% endfor %}
how can I get values item2 and item3 ?
英文:
I have 3 list item1,item2,item3
views.py
context={'item0': region.objects.all(),
'item1': [1,2,3,4,5,6,7,8,9,10],
'item2': ['one', 'two','three','four', 'five'],
'item3': ['six', 'seven','eight','nine','ten'],
'item4': "test",
'item5': 5,
'item6': range(5),
}
return render(request, 'btscreate.html', context )
I tried get in template
1)
{% for num in item1 %}
<td> {{ num }} </td> # this work
<td> {{ how can I get values item2 and item3 ? }} </td>
{% endfor %}
2)
{% for num in item6 %}
<td> {{ item2.0 }} </td> # that work
<td> {{ item2.num }} </td> # but that doesn't work
{% endfor %}
how can I get values item2 and item3 ?
答案1
得分: 1
你正在尝试的目标,据我所知,不可能在模板中实现。
为了确保我们理解一致,我将展示Python代码,你希望将其翻译成"Django模板语法"。
r = range(3)
list1 = ["ape", "whale", "giraffe"]
list2 = ["brown", "blue", "yellow"]
for idx in r:
print(list1[idx], list2[idx])
解决方案1:无需使用for循环并访问每个项目。这可能非常繁琐,但仍然是一个选择。
<table>
<tr>
<td> {{ item2.1 }} </td>
<td> {{ item3.1 }} </td>
</tr>
<tr>
<td> {{ item2.2 }} </td>
<td> {{ item3.2 }} </td>
</tr>
</table>
解决方案2:正如我之前所说,无法在模板语法中同时迭代两个列表,就像我的Python示例中所示。你需要在视图中使用zip
来合并这些列表:
views.py:
item2 = ['one', 'two', 'three', 'four', 'five']
item3 = ['six', 'seven', 'eight', 'nine', 'ten']
context={'item0': region.objects.all(),
'item1': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
'item23': zip(item2, item3),
'item4': "test",
'item5': 5,
'item6': range(5),
}
然后在你的模板中可以这样使用:
temp.html:
<table>
{% for i2, i3 in item23 %}
<tr>
<td> {{ i2 }} </td>
<td> {{ i3 }} </td>
</tr>
{% endfor %}
</table>
如果你的问题目标只是如何显示循环的索引,你可以这样做:
temp.html:
<table>
{% for i2, i3 in item23 %}
<tr>
<td> {{ forloop.counter0 }} </td>
<td> {{ i2 }} </td>
<td> {{ i3 }} </td>
</tr>
{% endfor %}
</table>
你不能将这个forloop计数器用作上述我的Python示例中其他列表的索引。
英文:
What you are trying to achieve is, as far as I know, not possible within templates.
To make sure we are on the same page I will show the code in python that you want to translate to "django-template-syntax"
r = range(3)
list1 = ["ape", "whale", "giraffe"]
list2 = ["brown", "blue", "yellow"]
for idx in r:
print(list1[idx], list2[idx]
Solution 1:
Do it without a for-loop and access every item. Might be very tedious but still an option.
<table>
<tr>
<td> {{ item2.1 }} </td>
<td> {{ item3.1 }} </td>
</tr>
<tr>
<td> {{ item2.2 }} </td>
<td> {{ item3.2 }} </td>
</tr>
</table>
Solution 2:
As I said iterating over two lists simultaneously like shown in my python example is not possible within the templating-syntax. You need to zip
those lists inside your views:
views.py:
item2 = ['one', 'two','three','four', 'five']
item3 = ['six', 'seven','eight','nine','ten']
context={'item0': region.objects.all(),
'item1': [1,2,3,4,5,6,7,8,9,10],
'item23': zip(item2, item3)
'item4': "test",
'item5': 5,
'item6': range(5),
}
Then in your template you do it like this:
temp.html:
<table>
{% for i2, i3 in item23 %}
<tr>
<td> {{ i2 }} </td>
<td> {{ i3 }} </td>
</tr>
{% endfor %}
</table>
If your question's target is just how to display the index of the loop you can do like this:
temp.html:
<table>
{% for i2, i3 in item23 %}
<tr>
<td> {{ forloop.counter0 }} </td>
<td> {{ i2 }} </td>
<td> {{ i3 }} </td>
</tr>
{% endfor %}
</table>
You can not use that forloop counter as an index for other lists similar to my python example above.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论