学习SSTIflask模版介绍
FLYFISH
视图函数
作用:生成请求的响应
视图函数只处理业务逻辑和数据处理
使用模版使用静态的页面html展示动态的内容
Flask模版
作用:取得视图函数的数据进行展示,返回响应内容
render_template
**作用:**加载html文件。默认文件路径再templates目录
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| from flask import Flask,render_template app=Flask(__name__) @app.route('/',methods=['GET'])
def index(): my_str='hello flyfish' my_age=18 my_word=request.args.get('flyfish') return render template("index.htm!", my_str=my_str, my_age=my_age, my_word=my_word ) if __name__=='__main__': app.run()
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> 模版页面 <br> {{ my_str }} <br> {{ my_word }} <br> {% set a='flyfish' %} {{ a }} </body> </html>
|
render_template_string
**作用:**用于渲染字符串,直接定义内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| from flask import Flask,render_template_string,request app=Flask(__name__) @app.route('/',methods=['GET'])
def index(): my_str='hello flyfish' my_age=18 my_word=request.args.get('flyfish') return render_template_string('<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title></head><body>模版页面<br>%s</body></html>'%my_word) if __name__=='__main__': app.run()
|