1. 程式人生 > 其它 >Django模板語句(三)

Django模板語句(三)

for 迴圈用到的一些引數

forloop.counter        #當前迴圈的索引值(從1開始)

for loop.counter0     #當前迴圈的索引值(從0開始)

forloop.revcounter   #當前迴圈的倒序索引值(從1開始)

forloop.revcounter 0   #當前迴圈的倒序索引值(從0開始)

forloop.first          #當前迴圈是不是第一次迴圈 (布林值)

forloop.last         #當前迴圈是不是最後一次迴圈 (布林值)

forloop.parentloop   #本層迴圈的外層迴圈

for .....empty  例子:

{% for book in book_list %}
<tr>
<td>{{ book.id }}</td>
<td>{{ book.name }}</td>
<td>{{ book.type }}</td>
<td>{{ book.Publisher.name }}</td>
<td>
<a class="btn btn-info" href="/egit_book/?id={{ book.id }}">編輯</a>
<a class="btn btn-danger" href="/delete_book/?id={{ book.id }}">刪除</a>
</td>
</tr>
{% empty %}
<tr>
<td colspan="5" class="text-center">暫時沒有資料哦~</td>
</tr>
{% endfor %}
with語句 例子:
{{name_list.1.1}} #過長時
{% with name =name_list.1.1%}
{{name}} #可以用name代替 name——list.1.1
{%endwith%}