【发布时间】:2021-12-23 11:42:08
【问题描述】:
我正在使用 Django,并希望遍历两个列表,以便它们并排: 我的意见文件:
def displayDict(request):
data = ["a", "b", "c"]
data2 = ["x", "y", "z"]
return render(request, 'chattr.html',
{'range': range(0,len(data)-1),'dictItems': data, "otherDict" : "other_bot", "dictItems_bot": data2, "otherDict2": "bot" , "duo" : (data, data2)})
我的模板:
{% for i in range %}
<p>
{{i}} <br>
<b>{{otherDict}}:</b> {{dictItems.i}} <br>
<b>{{otherDict2}}:</b> {{dictItems_bot.i}} <br>
{% comment %} {{a|add:1}} {% endcomment %}
</p>
{% endfor %}
我想要一个如下所示的网页:
other_bot: 'a'
dictItems_bot: 'x'
other_bot : 'b'
dictItems_bot: 'y'
other_bot: 'c'
dictItems_bot: 'z'
目前除了机器人名称之外没有任何渲染:
other_bot:
dictItems_bot:
other_bot:
dictItems_bot:
我也可以使用元组来完成这个内部循环,Django Template: looping through two lists。但这会复杂得多,...
【问题讨论】: