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閱讀 789評論 0 6
  • Python簡介 Python歷史 Python 是由 Guido van Rossum 在八十年代末和九十年代初...
    莫名其妙的一生閱讀 1,072評論 0 2
  • 字典:當索引不好用時 字典是一種通過名字引用值的數據結構。這種結構類型稱為映射。字典是Python中唯一內建的映射...
    mydre閱讀 512評論 0 0
  • 最近的項目需要導出文件,導出的文件里有數據分析圖,如折線圖,柱狀圖,散點圖等。綜合考慮之后,我選擇了目前已經很成熟...
    呆呆的木木閱讀 94,953評論 17 23