Flask初级(七)flash模板循环,判断


Project name :Flask_Plan

templates:templates

static:static

继续前面的代码

修改Flask_Plan.py

@app.route('/')
def hello_world():
    plan = [
        {
            'date':'20171212',
            'train':'T198',
            'track':'8'
        } ,
        {
            'date':'20171212',
            'train':'T199',
            'track':'9'
        } ,
        {
            'date':'20171212',
            'train':'T197',
            'track':'7'
        } ,
        {
            'date':'20171212',
            'train':'T196',
            'track':'6'
        }
    ]

    return render_template('plan.html', plans = plan)
View Code

修改templates/plan.html 

{% extends 'base.html' %}
{% block main2 %}
    <hr>
    {% for plan in plans %}
        {{ plan.date }}
        {{ plan.train }}
        {{ plan.track }}
        <br>
    {% endfor %}
{% endblock %}
View Code

我们在视图函数中定义了一个嵌套字典的列表,并以plans的列表名传入了模板plan.html

在模板中使用

{%  for plan in plans%}

{% end for%}

来创建循环,循环输出列表当中的字典的值。

 

有了循环,也要有判断

修改templates/plan.html 

{% extends 'base.html' %}
{% block main2 %}
    <hr>
    {% for plan in plans %}
        {{ plan.date }}
        {{ plan.train }}
        {% if plan.track == '8' %}
            8888
        {% elif plan.track =='9' %}
            9999
        {% else %}
            {{ plan.track }}
        {% endif %}
        <br>
    {% endfor %}
{% endblock %}
View Code

{% if plan.track == '8' %}
8888
{% elif plan.track =='9' %}
9999
{% else %}
{{ plan.track }}
{% endif %}

这样的格式来使用判断。

基本和python中一样。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM