# coding:utf-8 from flask import Flask, render_template app = Flask(__name__) @app.route('/') @app.route('/<name>') def index(name=None): # 使用render_template()來渲染模板 參數第一個爲模板頁面,第二個爲向模板傳入的參數,若是爲多個值依次傳入 return render_template('index.html', name=name) if __name__ == "__main__": app.run(host='0.0.0.0', port=12345, debug=True)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>title</title> </head> <body> {% if name %} <h1>Hello {{ name }} </h1> {% else %} <h1>Hello World </h1> {% endif%} </body> </html>
render_template('index.html', name=name, age=18 .....) 傳入多少均可以
def index(name=None): data = { 'name': name, 'age': 18 } return render_template('index.html', data)
# 將字典拆包的方式傳入方法中 return render_template('index.html', **data)
def index(name=None): data = { 'name': name, 'age': 18, 'my_dict': {'city': 'harbin'}, 'my_li': [1, 2, 3, 4, 5], } return render_template('index.html', **data)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>title</title> </head> <body> {% if name %} {# {{ 寫入傳入的變量名 }} #} <h1>Hello {{ name }} </h1> <hr> <h1>age {{ age }} </h1> <hr> {# 第一種獲取字典的方式 #} <h1>my_dict {{ my_dict['city'] }} </h1> <hr> {# 第二種獲取字典的方式 #} <h1>my_dict {{ my_dict.city }} </h1> <hr> {# 獲取列表的方式 #} <h1>my_li {{ my_li }} </h1> <hr> {# 遍歷列表 #} {% for li in my_li %} <h1>my_li {{ li }} </h1> {% endfor %} <hr> {# 獲取列表中的單個值,這裏獲取的是第一個元素 #} <h1>my_li {{ my_li[0] }} </h1> <hr> {% else %} <h1>Hello World </h1> {% endif%} </body> </html>
<h1>my_li[0] + my_li[1] = {{ my_li[0] + my_li[1] }} </h1> <h1>{{ "hello" + "worlds" }} </h1>
safe:禁用轉義html
<p>{{ '<em>hello</em>' | safe }}</p>flask
capitalize:把變量值的首字母轉成大寫,其他字母轉小寫;api
<p>{{ 'hello' | capitalize }}</p>瀏覽器
lower:把值轉成小寫;安全
<p>{{ 'HELLO' | lower }}</p>app
upper:把值轉成大寫;ide
<p>{{ 'hello' | upper }}</p>函數
title:把值中的每一個單詞的首字母都轉成大寫;spa
<p>{{ 'hello' | title }}</p>debug
trim:把值的首尾空格去掉;
<p>{{ ' hello world ' | trim }}</p>
reverse:字符串反轉;
<p>{{ 'olleh' | reverse }}</p>
format:格式化輸出;
<p>{{ 'name = %s , age = %d' | format('name',17) }}</p>
striptags:渲染以前把值中全部的HTML標籤都刪掉;
<p>{{ '<em>hello</em>' | striptags }}</p>
<p>{{ 「 hello world 「 | trim | upper }}</p>
first:取第一個元素
<p>{{ [1,2,3,4,5,6] | first }}</p>
last:取最後一個元素
<p>{{ [1,2,3,4,5,6] | last }}</p>
length:獲取列表長度
<p>{{ [1,2,3,4,5,6] | length }}</p>
sum:列表求和
<p>{{ [1,2,3,4,5,6] | sum }}</p>
sort:列表排序
<p>{{ [6,2,3,1,5,4] | sort }}</p>
1 # coding:utf-8 2 3 from flask import Flask, render_template 4 5 app = Flask(__name__) 6 7 8 @app.route('/') 9 @app.route('/<name>') 10 def index(name=None): 11 data = { 12 'name': name, 13 'age': 18, 14 'my_dict': {'city': 'harbin'}, 15 'my_li': [1, 2, 3, 4, 5], 16 } 17 return render_template('index.html', **data) 18 19 20 def list_step_2(li): 21 """自定義過濾器""" 22 return li[::2] 23 24 25 # 註冊過濾器 26 app.add_template_filter(list_step_2, 'li2') 27 28 29 if __name__ == "__main__": 30 app.run(host='0.0.0.0', port=12345, debug=True)
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>title</title> 6 </head> 7 <body> 8 <h1>過濾my_li = {{ my_li | li2 }}</h1> 9 </body> 10 </html>
1 # coding:utf-8 2 3 from flask import Flask, render_template 4 5 app = Flask(__name__) 6 7 8 @app.route('/') 9 @app.route('/<name>') 10 def index(name=None): 11 data = { 12 'name': name, 13 'age': 18, 14 'my_dict': {'city': 'harbin'}, 15 'my_li': [1, 2, 3, 4, 5], 16 } 17 return render_template('index.html', **data) 18 19 20 # 第一種方式添加過濾器 21 def list_step_2(li): 22 """自定義過濾器""" 23 return li[::2] 24 25 26 # 第二種凡是添加過濾器 27 @app.template_filter("fir") 28 def list_first_element(li): 29 return li[0] 30 31 32 # 註冊過濾器 33 app.add_template_filter(list_step_2, 'li2') 34 35 36 if __name__ == "__main__": 37 app.run(host='0.0.0.0', port=12345, debug=True)
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>title</title> 6 </head> 7 <body> 8 <h1>第1種添加過濾器方式 過濾my_li = {{ my_li | li2 }}</h1> 9 <h1>第2種添加過濾器方式 過濾my_li = {{ my_li | fir }}</h1> 10 </body> 11 </html>