from flask import Flask,request, jsonify, redirect, url_for,abort,make_response,session,render_template app = Flask(__name__) @app.route('/') def index(): POST ={ 'title':'去去去', 'age':18, 'height':1.79 } my_string = 'welcome for you' my_int = 10 my_list =[1,2,3,4,5]
#模版渲染 return render_template('muban.html',POST=POST, my_string = my_string, my_int = my_int, my_list = my_list) if __name__ == '__main__': app.run(debug=True)
muban.html {#<h1>{{ 这是变量代码块 }}</h1>#} <h1>{{ POST.title}}</h1> {{ POST.age }}<br> {{ POST.height }}<br> {{ my_string }}<br> {{ my_int }}<br> {{ my_list }}<br>
muban.html 对py代码进行渲染
filter.html 放在templates文件夹下
<!--禁止转义--> <p>{{ '<em>hello</em>' | safe }}</p> {{ 'hello '| capitalize }}<!--首字母大写--> <br> {{ 'HELLO '| lower }}<!--转换为大写--> <br> {{ 'hello '| upper }}<!--转换为小写--> <br> <!--每个字符首写字母转化为大写--> {{ 'weger rge dsge' | title}} <br> <!--反转--> {{ 'agvb'| reverse }} <br> <!--格式化输出--> {{ '%s is %d'|format('name', 15) }} <br> <!--字符串截断--> {{ 'helloefvfb sge sg n ' | truncate(12)}}
from flask import Flask,request, jsonify, redirect, url_for,abort,make_response,session,render_template app = Flask(__name__) @app.route('/index') def index(): return render_template('filter.html') if __name__ == '__main__': app.run(debug=True)
truncate
{#<h1>{{ 这是变量代码块 }}</h1>#} {{ 'hellofwddsdfsgbb'| truncate(8, killwords=True)}} {#truncate(参数1,参数2,参数3)#} {#参数1 : 需要截取的长度#} {#参数2 :killwords=False时,以空格为分隔符进行截取,取截取的第一个,再加...#} {#参数3 :leeway 默认为5#} {#当前字符串的长度<= 参数1+参数3时,保留输入参数#} {#否则截取原字符串前(参数1)-3个字符,后面加上...#} {#{{ 'he llo fwddsdfsgbb'| truncate(8, killwords=False,leeway =0)}}#} {#he...#} {#{{ 'hellofwddsdfsgbb'| truncate(8, killwords=True,leeway =0)}}#} {#hello...#}