flask的模板和过滤器
#_*_ encoding: utf-8 _*_ @author: ty hery 2019/12/20
from flask import Flask, render_template, request
app = Flask(__name__)
@app.route('/index')
def index():
data = {
'name':'python01',
'age':19,
'my_dict':{'city':'sz'},
'my_list':[4,5,3,6,2,1],
'my_int':0
}
# return render_template("11_index.html",name='python',age=18)
return render_template("11_index.html",**data)
def list_step_2(li):
'''自定义的过滤器'''
return li[::2]
# 注册过滤器
app.add_template_filter(list_step_2,'li2')
@app.template_filter('li3')
def list_step_3(li):
'''自定义的过滤器'''
return li[::3]
@app.route('/xss',methods=['GET','POST'])
def xss():
text = ''
if request.method =='POST':
text = request.form.get('text')
return render_template('11_xss.html',text=text)
if __name__ == '__main__':
# print('--哈哈01--',app.url_map,'--哈哈01--')
app.run(debug=True)
# app.run(debug=False)
11.index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p>name= {{ name }}</p>
<p>age= {{ age }}</p>
<p>my_dict: city = {{ my_dict['city'] }}</p>
<p>my_dict: city = {{ my_dict.city }}</p>
<p>my_list: {{ my_list }}</p>
<p>my_list_first: {{ my_list| first }}</p>
<p>my_list_last: {{ my_list| last }}</p>
<p>my_list_lenth: {{ my_list| length }}</p>
<p>my_list_sum: {{ my_list| sum }}</p>
<p>my_list_sort: {{ my_list| sort }}</p>
<p>my_list_sort: {{ [6,2,5,1,3,2,0,7]| sort }}</p>
<p>my_list[my_int]: {{ my_list[my_int] }}</p>
<p>my_list01: {{ my_list }}</p>
<p>my_list[0] + my_list[1] = {{ my_list[0] + my_list[1] }}</p>
<p>{{ 'hello' + ' python' }}</p>
<p>{{ '<em>hello</em>' }}</p>
<p>{{ '<em>hello</em>'|safe }}</p>
<p>'hello'首字母转化为大写capitalize:{{ 'hello'| capitalize }}</p>
<p>'HELLO'全部转化为小写lower:{{ 'HELLO'| lower }}</p>
<p>'hello'全部转化为大写upper:{{ 'hello'| upper }}</p>
<p>每个单词的首字母大写title:{{ 'hello world I want to kill you'| title }}</p>
<p>去掉值的首尾空格trim:{{ ' hello flask '| trim |upper}}</p>
<p>字符串反转:{{ 'hello world I want to kill you'| reverse }}</p>
<p>格式化输出format:{{ '%s is %d'| format('name',17) }}</p>
<hr>
<P>原始的{{ my_list }}</P>
<P>{{ my_list| li2 }}</P>
<P>{{ my_list| li3 }}</P>
</body>
</html>
11.xss.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form method = 'post'>
<textarea name="text"></textarea>
<input type="submit" value="提交">
</form>
{{ text | safe }}
</body>
</html>
写入自己的博客中才能记得长久