from flask import Flask, render_template
app = Flask(__name__)
@app.route('/result')
def result():
dict = {'phy':50,'che':60,'maths':70}
return render_template('hello.html', result = dict)
if __name__ == '__main__':
app.run(debug = True)
基础代码
从flask中通过模板返回html代码from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return render_template(‘hello.html’) #会从templates目录下寻找 hello.html
if __name__ == '__main__':
app.run(debug = True)
render_template函数,开发人员在开发flask的过程中,如果使用jinga2的引擎,应当注意避免SSTI漏洞
相关漏洞学习网站如下:
https://morblog.cc/posts/2946623354/
Jinga2模板引擎使用以下分隔符从HTML转义。
-
控制结构: {%...%}用于语句
-
变量取值: {{...}}表达式可以打印到模板输出
-
注释: {#...#}用于注释未包含在模板输出中
-
#... ##用于行语句
这里的input type是button,按钮,onclik则是按钮触发后触发的函数,这段代码的解析需要具有html和js的一般基础。
页面form提交跳转在以下示例中, '/' URL呈现具有表单的网页(student.html)。 填充的数据将发布到'/result' result'URL,触发result()函数。
code
from flask import Flask, render_template, request
app = Flask(__name__)
@app.route('/')
def student():
return render_template('student.html')
@app.route('/result',methods = ['POST', 'GET'])
def result():
if request.method == 'POST':
result = request.form
return render_template("result.html",result = result)
if __name__ == '__main__':
app.run(debug = True)
html代码解析:
比如说这里的话就会把页面送到form_action.asp页面。
这里把form提交给了result
result.html代码如下
| {{ key }} | {{ value }} |
|---|
用了循环语句 emmmm,前端确实还挺好玩的
cookie在Flask中,cookie设置在响应对象上。 使用make_response()函数从视图函数的返回值中获取响应对象。 之后,使用响应对象的set_cookie()函数来存储cookie。
SessionsSessions存储 在服务器上,想必打ctf的小伙伴都有了解,。 会话是客户端登录服务器并注销服务器的时间间隔。 需要在此会话中保存的数据存储在服务器上的临时目录中。 为每个客户端的Session ID分配Session ID 。 会话数据存储在cookie之上,服务器以加密方式对其进行签名。 对于此加密,Flask应用程序需要定义的SECRET_KEY 。
Session对象也是一个字典对象,包含会话变量和关联值的键值对。
例如,要设置'username'会话变量,请使用以下语句 -
Session[‘username’] = ’admin’ 12
要释放会话变量,请使用pop()方法。
session.pop('username', None)



