1. 程式人生 > >django標籤-for標籤

django標籤-for標籤

views.py

def index(request):
    context={'books':['紅樓夢','西遊記','水滸傳','三國演義'],
             'person':{'name':'alice','age':18,'hobby':'tennis'}}
    return render(request,'index.html',context=context)

 

index.html

……
<body>
{% for book in books %}
    <ul>  
      <li>{{ book }}</li>
    </ul>
{
% endfor %} {% for key in person.keys %} <ul> <li>{{ key }}</li> </ul> {% endfor %} </body> ……

結果顯示

reversed表示反向遍歷,即如果for book in books reversed,則會從列表的最後一個值向前依次遍歷