繁体   English   中英

django模板语法迭代两个列表

[英]django template syntax to iterate through two lists

我有以下django模板,当我迭代列表( class_list_overall )时,我想使用forloop.counter0作为另一个列表中的索引( classTimeSlots )。 它只是给我一个TemplateSyntaxError 我尝试了以下变化:

  1. {{classTimeSlots.{{forloop.counter0}}}}
  2. {{classTimeSlots.[forloop.counter0]}}
  3. {{classTimeSlots.{forloop.counter0}}}
  4. {{classTimeSlots.(forloop.counter0)}}
  5. {{classTimeSlots.forloop.counter0}}
  6. {% with forloop.counter0 as index%} <legend>{{ classTimeSlots.index}}</legend> {% endwith %}

这些都没有奏效。 有什么建议么? 我只是DJango的新手。 我正在使用Google App Engine。

这是代码片段(我知道它效率低但我一直在尝试不同的东西):

{% 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 %}

简短的回答:你做不到。

模板语言不会尝试确定以点语法传递的变量的值。

它将对forloop.counter0进行文字查找

1:编写一个接受变量和键的模板标签,并让它返回variable[key]

2:这很可能在视图中完成。 我可以看吗?

Django不支持这一点 - 它是故意限制的。 相反,您应该修改视图函数以将两个列表zip在一起,并将其传递给模板。

它可能但不值得推荐:

{% 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 %}

但最好将列表纳入父词典:

[class_list_overall[i].update({'label':classTimeSlots[i]}) for i in range(0,len(classTimeSlots))]

然后将上面的代码更改为:

<legend>
   {{ class_list.label }}
</legend>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM