flask第二十六篇——模板【控制语句】【2】

如果你也在学flask,就请加船长的公众号:自动化测试实战

 

我们先补充一下for循环的知识,我们之前说过,flask是由Jinja2+sqlAlchemy+werkzeug组成的,我们现在学的控制语句就属于Jinja的知识了。
Jinja2中的for循环包含以下变量,用来获取当前遍历的状态:

变量描述
loop.index 当前迭代的索引(从1开始)
loop.index0 当前迭代的索引(从0开始)
loop.first 是否是第一次迭代,返回True/False
loop.last 是否是最后一次迭代,返回True/False
loop.length 序列的长度


比如我们判断如果是最后一次迭代,那么就把那一行标红,就可以这么写:

<h3>遍历中的变量</h3>
<table>
    <thead>
        <th>姓名</th>
        <th>年龄</th>
    </thead>
    <tbody>
        {% for user in users %}
            <p>{{ loop.index }}</p>
            {% if loop.last %}
                <tr style="background: firebrick;">
            {% else %}
                <tr>
            {% endif %}
                <td>{{ user.name }}</td>
                <td>{{ user.age }}</td>
            </tr>

        {% endfor %}

    </tbody>
</table>

 

此外,在flask中,是没有continuebreak来中断for循环的,那我们要停止for循环该怎么办呢?我们就需要借助if进行判断了,比如下面的代码,我先循环整个users列表,然后判断,如果name是Warren,name就用表格打印出姓名和年龄:

# coding: utf-8

from flask import Flask, render_template

app = Flask(__name__)  # type: Flask
app.debug = True

@app.route('/')
def login():
    context = {
        "users": [
            {
                "name": "Warren",
                "age": 18
            },
            {
                "name": "Captain",
                "age": 20
            }
        ],
        "groups": {
                "math": "perfect",
                "Chinese": "Great"
        }
    }
    
    return render_template('index.html', **context)


if __name__ == '__main__':
    app.run()

 

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>控制语句</title>
</head>
<body>
    {# 如果姓名name是Warren,就打印名字,否则打印其他信息 #}
    {% if users[0].name == "Warren" %}
        <p>{{ users[0].name }}</p>
        <p>用户名是Warren</p>
    {% else %}
        <p>用户名不是Warren</p>
    {% endif %}

    {# for循环 #}
    <h3>遍历列表</h3>
    <table>
        <thead>
            <th>姓名</th>
            <th>年龄</th>
        </thead>
        <tbody>
            {% for user in users %}
                <tr>
                    <td>{{ user.name }}</td>
                    <td>{{ user.age }}</td>
                </tr>

            {% endfor %}
        </tbody>
    </table>

    <h3>遍历字典</h3>
    {% for key, value in groups.items() %}
        <p>key: {{ key }};value: {{ value }}</p>
    {% endfor %}

    <h3>遍历中的变量</h3>
    <table>
        <thead>
            <th>姓名</th>
            <th>年龄</th>
        </thead>
        <tbody>
            {% for user in users %}
                <p>{{ loop.index }}</p>
                {% if loop.last %}
                    <tr style="background: firebrick;">
                {% else %}
                    <tr>
                {% endif %}
                    <td>{{ user.name }}</td>
                    <td>{{ user.age }}</td>
                </tr>

            {% endfor %}

        </tbody>
    </table>

    <h3>中途断开for循环</h3>
    <table>
        <thead>
            <th>姓名</th>
            <th>年龄</th>
        </thead>
        <tbody>
            {% for user in users if user.name == 'Warren' %}
                <tr>
                    <td>{{ user.name }}</td>
                    <td>{{ user.age }}</td>
                </tr>
            {% endfor %}
        </tbody>
    </table>

</body>
</html>

 

最后执行代码,可以看到:

大家需要自己动手写一下,这样才能记得深刻。

 


 

今天我们给大家留个作业,就是用学的for循环写一个九九乘法表,明天我们会公布答案,大家可以自己试一下,看看自己学的怎么样~~

 

如果觉得有帮助,别忘了动动小手,点个赞哦~~

posted @ 2018-04-20 15:48  孟船长  阅读(1138)  评论(0编辑  收藏  举报