Python 在django中的模板循环中使用列表索引查找

Python 在django中的模板循环中使用列表索引查找,python,django,list,django-templates,Python,Django,List,Django Templates,基本上,我想做的是让模板系统循环通过两个独立的列表来填充表的两列。我的方法是使用索引列表(numList)作为访问两个列表的相同索引的方法。我尝试在模板循环中使用点表示法查找列表索引,但在循环中似乎不起作用。有什么办法可以补救吗 numList = [0, 1, 2, 3] placeList = ['park', 'store', 'home', 'school'] speakerList = ['bill', 'john', 'jake', 'tony'] <tabl

基本上,我想做的是让模板系统循环通过两个独立的列表来填充表的两列。我的方法是使用索引列表(numList)作为访问两个列表的相同索引的方法。我尝试在模板循环中使用点表示法查找列表索引,但在循环中似乎不起作用。有什么办法可以补救吗

numList = [0, 1, 2, 3]
placeList = ['park', 'store', 'home', 'school']
speakerList = ['bill', 'john', 'jake', 'tony']

        <table>
            <tr>
                <th>Location</th>
                <th>Time</th>
                <th>Speaker</th>
            </tr>
            {% for num in numList %}
             <tr>
                <td>{{ placeList.num }}</td>
                <td>1:30</td>
                <td>{{ speakerList.num }}</td>
             </tr>
             {% endfor %}
        </table>
numList=[0,1,2,3]
placeList=[“公园”、“商店”、“家”、“学校”]
speakerList=['bill','john','jake','tony']
位置
时间
演讲者
{numList%中num的%s}
{{placeList.num}
1:30
{{speakerList.num}
{%endfor%}

您可以将这两个列表合并到一个列表中

例如:

yourlist = [('park','bill'),('store','john'),('home','jake'),...]
combinedList = [{'place':placeList[i],'speaker':speakerList[i]} for i in range(4)]

{% for entry in combinedList %}
<tr>
<td>{{ entry.place }}</td>
<td>1:30</td>
<td>{{ entry.speaker }}</td>
</tr>
{% endfor %}

最简单的方法可能是在python中组合列表,然后在模板中查看组合列表:

combinedList = [(placeList[i],speakerList[i]) for i in range(4)]

{% for entry in combinedList %}
<tr>
<td>{{ entry.0 }}</td>
<td>1:30</td>
<td>{{ entry.1 }}</td>
</tr>
{% endfor %}
combinedList=[(placeList[i],speakerList[i]),用于范围(4)中的i)
{combinedList%中条目的%s}
{{entry.0}
1:30
{{entry.1}}
{%endfor%}
或者,为了透明,您可以将combinedList设置为对象或字典的列表,例如:

yourlist = [('park','bill'),('store','john'),('home','jake'),...]
combinedList = [{'place':placeList[i],'speaker':speakerList[i]} for i in range(4)]

{% for entry in combinedList %}
<tr>
<td>{{ entry.place }}</td>
<td>1:30</td>
<td>{{ entry.speaker }}</td>
</tr>
{% endfor %}
combinedList=[{'place':placeList[i],'speaker':speakerList[i]}用于范围(4)中的i)
{combinedList%中条目的%s}
{{entry.place}
1:30
{{entry.speaker}}
{%endfor%}
combinedList=zip(placeList,speakerList)
更好