我有我的模板下面的代碼:如何獲取django模板循環中第二,第三,第n遍的值?
{% with element=vals|first %} #can use last here to get the last value
{% for key,value in element.items %}
<td>{{ value.corr }}</td>
<td>{{ value.str }}</td>
<td>{{ value.sig }}</td>
{% endfor %}
{% endwith %}
這將使我在循環的第一次迭代的第一個值。
如何獲得第二,第三和第n?
我想過使用切片,但這似乎並沒有在這裏工作 - 或者我沒有正確格式化切片。
請幫忙!
編輯:我http://stardict.sourceforge.net/Dictionaries.php下載列表:
[{('age', 'after'):
{'str': 'one', 'sig': 'two', 'cor': 'three'}
},
{('age', 'before'):
{'str': 'zero', 'sig': 'one', 'cor': 'four'}
},
{('exp', 'after'):
{'str': 'one', 'sig': 'two', 'cor': 'three'}
},
{('exp', 'before'):
{'str': 'zero', 'sig': 'one', 'cor': 'four'}
}]
編輯2:所需的輸出
<table class="table table-striped table-hover">
<thead>
<tr>
<th>Parameters</th>
<th>Pos/Neg</th>
<th>Str</th>
<th>Sig</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">First Table Row</th>
{% with element=q3vals|first %}
{% for key,value in element.items %}
{% if forloop.counter == 1 %}
<td>{{ value.corr }}</td>
<td>{{ value.str }}</td>
<td>{{ value.sig }}</td>
{% endif %}
{% endfor %}
{% endwith %}
</tr>
<tr>#second loop through the list of dicts</tr>
<tr>#third loop through the list of dicts
<tr>
<th scope="row">Fourth Table Row</th>
{% with element=q3vals|last %} #last loop through
{% for key,value in element.items %}
<td>{{ value.corr }}</td>
<td>{{ value.str }}</td>
<td>{{ value.sig }}</td>
{% endfor %}
{% endwith %}
</tr>
</tbody>
</table>
如果任何回答以下問題的幫助你,然後將其標記爲接受的請。在StackOverflow中這是一個很好的做法:) –