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,则会从列表的最后一个值向前依次遍历

猜你喜欢

转载自www.cnblogs.com/Forever77/p/10126678.html