Skip to main content
 首页 » 编程设计

python之Django之在模板列表中获取元素索引

2025年05月04日25webabcd

<分区>

我在模板中有一个用于迭代的列表,迭代时,我想在这个列表中打印它的索引。现在我正在使用 list.index(element),但这不起作用。

我愿意:

<tbody> 
   {% for core in cores %} 
   <tr> 
       <td>{{cores.index(core)}}</td>     <!-- this line doesn't work --> 
       <td>{{core.user}}</td>             <!-- this line works. --> 

这不是办法吗?我必须做 for i in length(cores)?