python-Flask(jinja2)语法:判断与循环

逻辑与循环

[TOC]

if 语句

语法:

{% if xxx %}
{% else %}
{% endif %}

例子:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>index</title>
</head>
<body>
    {% if user and user.age > 18 %}
        <a href="#">{{ user.username }}</a>
        <a href="#">注销</a>
    {% else %}
        <a href="#">登录</a>
        <a href="#">注册</a>
    {% endif %}
</body>
</html>
@app.route('/<is_login>/')
def index(is_login):
    if is_login:
        user = {
            'username' : u'站长',
            'age' : 22
        }
        return render_template('index.html', user= user)
        # 已经注册则传进去参数
    else:
        return render_template('index.html') 
        # 没有注册则直接渲染

for循环遍历

字典遍历:语法和python一样,可以使用items()keys()values()iteritems()iterkeys()itervalues()

{% for k,v in user.items() %}
    <p>{{ k }}:{{ v }}</p>
{% endfor %}
# for遍历字典
@app.route('/')
def index():
    # 定义一个字典
    user = {
        'username' : u'站长',
        'age' : 22
    }
    return render_template('index.html',user=user)

列表遍历:,语法与python一样

{% for website in websites %}
    <p>{{ website }}</p>
{% endfor %}
# for遍历列表
@app.route('/')
def index():
    websites = ['www.baidu.com','www.google.com'] 
    return render_template('index.html',websites=websites)

例子:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>index</title>
</head>
<body>
    <p>综合运用列表和字典的模板文件</p>
    <table>
        <thead>
            <th>书名</th>
            <th>作者</th>
            <th>价格</th>
        </thead>
        <tbody>
            {% for book in books %}
                <tr>
                    <td>{{ book.name }}</td>
                    <td>{{ book.author }}</td>
                    <td>{{ book.price }}</td>
                </tr>
            {% endfor %}
        </tbody>
    </table>
</body>
</html>
#encoding: utf-8
from flask import Flask,render_template

app = Flask(__name__)

@app.route('/')
def index():
    books = [
        {
            'name' : u'西游记',
            'author' : u'吴承恩',
            'price' : 88
        },
        {
            'name': u'三国演义',
            'author': u'罗贯中',
            'price': 98
        },
        {
            'name': u'红楼梦',
            'author': u'曹雪芹',
            'price': 89
        },
        {
            'name': u'水浒传',
            'author': u'施耐庵',
            'price': 101
        }
    ]

    return render_template('index.html', books=books)

if __name__ == '__main__':
    app.run(debug=True)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Python 面向对象Python从设计之初就已经是一门面向对象的语言,正因为如此,在Python中创建一个类和对...
    顺毛阅读 4,238评论 4 16
  • 文/Bruce.Liu1 1.运算符 本章节主要说明Python的运算符。举个简单的例子 4 +5 = 9 。 例...
    BruceLiu1阅读 790评论 0 6
  • Python简介 Python历史 Python 是由 Guido van Rossum 在八十年代末和九十年代初...
    莫名其妙的一生阅读 1,072评论 0 2
  • 字典:当索引不好用时 字典是一种通过名字引用值的数据结构。这种结构类型称为映射。字典是Python中唯一內建的映射...
    mydre阅读 517评论 0 0
  • 最近的项目需要导出文件,导出的文件里有数据分析图,如折线图,柱状图,散点图等。综合考虑之后,我选择了目前已经很成熟...
    呆呆的木木阅读 94,972评论 17 23