代码1:for in 循环:
遍历列表
views.py文件:
from django.shortcuts import render
def index(request):
context = {
“books”:[
“三国演义”,
“红楼梦”,
“水浒传”,
“西游记”
]
}
return render(request,‘index.html’,context=context)
index.html文件:
- {% for book in books %}
- {{ book }}
- {% endfor %}
</ul>
反向遍历index.html:
- {% for book in books reversed %} ---使用reversed字段
- {{ book }}
- {% endfor %}
</ul>
遍历字典:
views.py代码:
from django.shortcuts import render
def index(request):
context = {
“books”:[
“三国演义”,
“红楼梦”,
“水浒传”,
“西游记”
],
“person”:{
“username”:“张三”,
“age”:18,
“height”:180
}
}
return render(request,‘index.html’,context=context)
index.html代码:
- {% for book in books reversed %}
- {{ book }}
- {% endfor %}
</ul> <ul> {% for key in person.values %} <li>{{ key }}</li> {% endfor %} </ul>
index.html文件:
- {% for book in books reversed %}
- {{ book }}
- {% endfor %}
</ul> <ul> {% for key,values in person.items %} ----遍历字典的键值对。 <li>{{ key }}/{{ values }}</li> {% endfor %} </ul>
遍历列表当中的字典:
index.html
- {% for key,values in person.items %}
- {{ key }}/{{ values }}
- {% endfor %}
</ul> <table> <thead> <tr> <td>书名</td> <td>作者</td> <td>价格</td> </tr> </thead> <tbody> {% for book in books %} <tr> <td>{{ book.name }}</td> <td>{{ book.author }}</td> <td>{{ book.price }}</td> </tr> {% endfor %} </tbody> </table>
view.py:文件:
from django.shortcuts import render
def index(request):
context = {
“books”:[
{
“name”:“三国演义”,
“author”:“罗贯中”,
“price”:100
},
{
“name”:“水浒传”,
“author”:“施耐庵”,
“price”:120
},
{
“name”:“西游记”,
“author”:“吴承恩”,
“price”:130
},
{
“name”:“红楼梦”,
“author”:“曹雪芹”,
“price”:200
}
],
“person”:{
“username”:“张三”,
“age”:18,
“height”:180
}
}
return render(request,‘index.html’,context=context)
给遍历的标签添加背景效果:
- {% for key,values in person.items %}
- {{ key }}/{{ values }}
- {% endfor %}
</ul> <table> <thead> <tr> <td>序号</td> <td>书名</td> <td>作者</td> <td>价格</td> </tr> </thead> <tbody> {% for book in books %} {% if forloop.first %} <tr style="background: pink"> {% elif forloop.last %} <tr style="background: blue"> {% else %} <tr> ------注意这里是开始的tr标签 {% endif %} <td>{{ forloop.counter }}</td> <td>{{ book.name }}</td> <td>{{ book.author }}</td> <td>{{ book.price }}</td> </tr> {% endfor %} </tbody> </table>
for …in…empty代码:
views.py代码
from django.shortcuts import render
def index(request):
context = {
“books”:[
{
“name”:“三国演义”,
“author”:“罗贯中”,
“price”:100
},
{
“name”:“水浒传”,
“author”:“施耐庵”,
“price”:120
},
{
“name”:“西游记”,
“author”:“吴承恩”,
“price”:130
},
{
“name”:“红楼梦”,
“author”:“曹雪芹”,
“price”:200
}
],
“person”:{
“username”:“张三”,
“age”:18,
“height”:180
},
“comments”:[ ------文章的评论
“文章不错”,
“写的好”
]
}
return render(request,‘index.html’,context=context)
index.html代码:
- {% for key,values in person.items %}
- {{ key }}/{{ values }}
- {% endfor %}
</ul> <table> <thead> <tr> <td>序号</td> <td>书名</td> <td>作者</td> <td>价格</td> </tr> </thead> <tbody> {% for book in books %} {% if forloop.first %} <tr style="background: pink"> {% elif forloop.last %} <tr style="background: blue"> {% else %} <tr> {% endif %} <td>{{ forloop.counter }}</td> <td>{{ book.name }}</td> <td>{{ book.author }}</td> <td>{{ book.price }}</td> </tr> {% endfor %} </tbody> </table> <ul> {% for comment in comments %} -------演示遍历评论,有就打印,没有打印没有评论 <li>{{ comment }}</li> {% empty %} <li>没有任何评论</li> {% endfor %} </ul>