[英]django template syntax to iterate through two lists
I have the following django template, where as I'm iterating through a list ( class_list_overall
), I want to use forloop.counter0
as an index in another list ( classTimeSlots
). 我有以下django模板,当我迭代列表( class_list_overall
)时,我想使用forloop.counter0
作为另一个列表中的索引( classTimeSlots
)。 It just keeps giving me a TemplateSyntaxError
. 它只是给我一个TemplateSyntaxError
。 I have tried the following variations: 我尝试了以下变化:
{{classTimeSlots.{{forloop.counter0}}}}
{{classTimeSlots.[forloop.counter0]}}
{{classTimeSlots.{forloop.counter0}}}
{{classTimeSlots.(forloop.counter0)}}
{{classTimeSlots.forloop.counter0}}
{% with forloop.counter0 as index%} <legend>{{ classTimeSlots.index}}</legend> {% endwith %}
None of which worked. 这些都没有奏效。 Any suggestions? 有什么建议么? I'm just a newbie at DJango. 我只是DJango的新手。 I'm using Google App Engine. 我正在使用Google App Engine。
Here's the code snippet (I know it's inefficient but I've been trying different things): 这是代码片段(我知道它效率低但我一直在尝试不同的东西):
{% for class_list in class_list_overall %}
<fieldset> <legend>{{ classTimeSlots.forloop.counter0 }}</legend>
<ul>
<li> <label>First Choice </label>
<select class="dropdown" name="class{{forloop.counter}}1" size="1">
<option value="Click Here to Choose" selected="selected">Click Here to Choose</option>
{% for class in class_list %}
<option>{{class}}</option>
{% endfor %}
</select>
</li>
<li>
<label>Second Choice </label>
<select class="dropdown" name="class{{forloop.counter}}2" size="1">
<option value="Click Here to Choose" selected="selected">Click Here to Choose</option>
{% for class in class_list %}
<option>{{class}}</option>
{% endfor %}
</select>
</li>
</ul>
</fieldset>
{% endfor %}
Short answer: you can't do that. 简短的回答:你做不到。
The template language will not try to determine the value of a variable passed in dot syntax. 模板语言不会尝试确定以点语法传递的变量的值。
It will do a literal lookup of forloop.counter0
它将对forloop.counter0
进行文字查找
1: write a template tag that accepts a variable and a key, and have it return the variable[key]
1:编写一个接受变量和键的模板标签,并让它返回variable[key]
2: this can most likely be done in the view. 2:这很可能在视图中完成。 Can I see it? 我可以看吗?
Django doesn't support this - it's deliberately limited. Django不支持这一点 - 它是故意限制的。 Instead, you should modify your view function to zip
the two lists together, and pass that in to the template. 相反,您应该修改视图函数以将两个列表zip
在一起,并将其传递给模板。
Its possible but not recommendable: 它可能但不值得推荐:
{% for class_list in class_list_overall %}
<fieldset> <legend>
{% for cts in classTimeSlots %}
{% ifequal forloop.counter forloop.parentloop.counter %} {{cts}}
{% endifequal %}
{% endfor %} </legend>
<ul>
<li> <label>First Choice </label>
<select class="dropdown" name="class{{forloop.counter}}1" size="1">
<option value="Click Here to Choose" selected="selected">Click Here to Choose</option>
{% for class in class_list %}
<option>{{class}}</option>
{% endfor %}
</select>
</li>
<li>
<label>Second Choice </label>
<select class="dropdown" name="class{{forloop.counter}}2" size="1">
<option value="Click Here to Choose" selected="selected">Click Here to Choose</option>
{% for class in class_list %}
<option>{{class}}</option>
{% endfor %}
</select>
</li>
</ul>
</fieldset>{% endfor %}
But its better to take list into parent dict: 但最好将列表纳入父词典:
[class_list_overall[i].update({'label':classTimeSlots[i]}) for i in range(0,len(classTimeSlots))]
And then change above code to: 然后将上面的代码更改为:
<legend>
{{ class_list.label }}
</legend>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.