27.for标签使用详解

for…in… 标签:for…in…类似于Python中的for…in…,可以遍历列表,元组,字符串,字典等一切可以遍历的对象,示例代码如下:
index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <ul>
        {% for book in books %}
            <li>{{ book }}</li>
        {% endfor %}
    </ul>
    <ul>
{#  访问键  #}
        {% for person in persons %}
            <li>{{ person }}</li>
        {% endfor %}
    </ul>
    <ul>
{#  访问键对应的值  #}
        {% for value in persons.values %}
            <li>{{ value }}</li>
        {% endfor %}
    </ul>
    <ul>
{#  键值对访问  #}
        {% for key,value in persons.items %}
            <li>{{ key }}-{{ value }}</li>
        {% endfor %}
    </ul>
</body>
</html>
views.py视图函数:

from django.shortcuts import render


def index(request):
    context = {
        'books':[
            '三国演义',
            '红楼梦',
            '水浒传',
            '西游记'
        ],
        'persons':{
            'username': '小蚂蚁',
            'age': 18,
            'height': 170
        },
    }
    return render(request,'index.html',context=context)
如果想要反向遍历,那么在遍历的时候就可以加上一个reversed。示例代码如下:
    <ul>
{#  可以反向遍历列表,但是不能反向遍历字典  #}
        {% for book in books reversed%}
            <li>{{ book }}</li>
        {% endfor %}
    </ul>

在浏览器中查看结果:
在这里插入图片描述

在for循环中,DTL提供了一些变量可供利用,这些变量如下:
  1. forloop.counter:当前循环的下标,以1作为起始值。
  2. forloop.counter0:当前循环的下标,以0作为起始值。
  3. forloop.revcounter:当前循环的反向下标值,比如列表有5个元素,那么第一次遍历这个属性是等于5,之后就是4,3, 2, 1,这是以1作为最后一个元素的下标。
  4. forloop.revcounter0:类似于forloop.revcounter.不同的是最后一个元素的下标是从0开始。
  5. forloop.first:是否是第一次遍历。
  6. forloop.last:是否是最后一次遍历。
  7. forloop.parentloop:如果有多个循环嵌套,那么这个属性代表的是上一级的for循环。
    示例代码如下:
index.html模板中:使用forloop.counter
<table>
    <thead>
    <tr>
        <td>序号</td>
        <td>书名</td>
        <td>作者</td>
        <td>价格</td>
    </tr>
    </thead>
    <tbody>
    {% for book in books %}
        <tr>
        <!--forloop.counter序号从1开始-->
            <td>{{ forloop.counter }}</td>
            <td>{{ book.name }}</td>
            <td>{{ book.author }}</td>
            <td>{{ book.price }}</td>
        </tr>
    {% endfor %}
    </tbody>
</table>
def index(request):
    context = {
    'books':[
            {
                'name': '三国演义',
                'author': '罗贯中',
                'price': 200
            },
            {
                'name':'西游记',
                'author':'吴承恩',
                'price':100
            },
            {
                'name':'红楼梦',
                'author':'曹雪芹',
                'price':299
            },
            {
                'name':'水浒传',
                'author':'施耐庵',
                'price':199
            },
        ]}
    return render(request,'index.html',context=context)
在index.html模板中使用forloop.first和forloop.last进行判断,并且对tr标签的颜色进行着色。示例代码如下:
<table>
    <thead>
    <tr>
        <td>序号</td>
        <td>书名</td>
        <td>作者</td>
        <td>价格</td>
    </tr>
    </thead>
    <tbody>
    {% for book in books %}
    <!--forloop.first-->
        {% if forloop.first %}
            <tr style="background: rebeccapurple;">
            <!--forloop.last-->
                {% elif forloop.last %}
            <tr style="background: pink;">
                {% else %}
            <tr style="background: silver">
        {% endif %}
                <td>{{ forloop.counter }}</td>
                <td>{{ book.name }}</td>
                <td>{{ book.author }}</td>
                <td>{{ book.price }}</td>
            </tr>
    {% endfor %}
    </tbody>
</table>

浏览器中展示结果如下:
在这里插入图片描述

3.for…in…empty标签:这个标签使用跟for…in…是一样的,只不过是在遍历的对象如果没有元素的情况下,会执行empty中的内容,示例代码如下:
index.html中:
<ul>
    {% for comment in comments %}
        <li>{{ comment }}</li>
    {% empty %}
        <li>没有任何评论</li>
    {% endfor %}
</ul>
views.py文件中:
def index(request):
    context = {
     'comments':[
                # '文章真好',
                # '作者写的太真实了,好评加一'
            ],
        }
    return render(request,'index.html',context=context)

浏览器中查看表示结果:
在这里插入图片描述

发布了76 篇原创文章 · 获赞 2 · 访问量 2755

猜你喜欢

转载自blog.csdn.net/zjy123078_zjy/article/details/103994697
今日推荐