对于web应用,客户端与服务端之间的数据交互至关重要。在 Flask 中由全局的 request 对象来提供这些信息。
1 Request对象属性
request中包含了前端发送过来的所有请求数据
- form和data是用来提取请求体数据
- 当前端通过存表单提交的时候content-type:urlencode的时候,所有数据已经被flask封装到了form里面。
- 如果时候form-data多类型数据,可以通过request.data获取
- 通过requset.form可以直接提取请求体中的表单格式的数据, 是一个类字典的对象
- 通过get方法只能拿到多个同名参数的第一个
Request对象的重要属性如下:
属性 | 说明 |
---|---|
form | post请求参数, 存储结构个args一致, 它是一个字典对象,包含表单参数及其值的键和值对。默认是接收post参数, 还可以接收PUT,PATCH参数 |
args | get请求参数,args是一个ImmutableMultiDict对象,类字典结构对象。解析查询字符串的内容,它是问号(?)之后的URL的一部分。 |
cookie | 保存Cookie名称和值的字典对象。 |
files | 与上传文件有关的数据。 |
method | 当前请求方式。 |
path | 路由中的路径 |
url | 完整请求地址 |
base_url | 去掉GET参数的URL |
get_json | 如果前端传的json,可以调此接口,自动把前端传的json转化为字典。使用要求前端传递的content-type:application/json |
2 表单数据
当前请求的 HTTP 方法可通过 method 属性来访问。通过:attr:~flask.request.form 属性来访问表单数据( POST 或 PUT 请求提交的数据)。
在以下示例中,’/’ URL会呈现具有表单的网页(student.html)。
填入的数据会发布到触发 result()函数的’/result’ URL。
results()函数收集字典对象中的request.form中存在的表单数据,并将其发送给result.html。
该模板动态呈现表单数据的HTML表格。
from flask import Flask, render_template
from flask import 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)
student.html的HTML脚本:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<form action = "http://localhost:5000/result" method = "POST">
<p>Name <input type = "text" name = "Name" /></p>
<p>Physics <input type = "text" name = "Physics" /></p>
<p>Chemistry <input type = "text" name = "chemistry" /></p>
<p>Maths <input type ="text" name = "Mathematics" /></p>
<p><input type = "submit" value = "submit" /></p>
</form>
</body>
</html>
result.html的HTML脚本:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<table border = 1>
{
% for key, value in result.items() %}
<tr>
<th> {
{
key }} </th>
<td> {
{
value }} </td>
</tr>
{
% endfor %}
</table>
</body>
</html>
运行Python脚本,并在浏览器中输入URL http://localhost:5000/
当点击提交按钮时,表单数据以HTML表格的形式呈现在result.html上
3 Cookies
Cookie以文本文件的形式存储在客户端的计算机上。其目的是记住和跟踪与客户使用相关的数据,以获得更好的访问者体验和网站统计信息。
Request对象包含Cookie的属性。它是所有cookie变量及其对应值的字典对象,客户端已传输。除此之外,cookie还存储其网站的到期时间,路径和域名。
在Flask中,对响应对象设置cookie。使用make_response()函数从视图函数的返回值获取响应对象。之后,使用响应对象的set_cookie()函数来存储cookie。
读回cookie很容易。request.cookies属性的get()方法用于读取cookie。
你可以通过 cookies 属性来访问 Cookies,用响应对象的 set_cookie 方法来设置 Cookies。请求对象的 cookies 属性是一个内容为客户端提交的所有 Cookies 的字典。如果你想使用会话,请不要直接使用 Cookies .
读取 cookies:
from flask import request
@app.route('/')
def index():
username = request.cookies.get('username')
# use cookies.get(key) instead of cookies[key] to not get a
# KeyError if the cookie is missing.
存储 cookies:
from flask import make_response
@app.route('/')
def index():
resp = make_response(render_template(...))
resp.set_cookie('username', 'the username')
return resp
Cookies 是设置在响应对象上的。由于通常视图函数只是返回字符串,之后 Flask 将字符串转换为响应对象。如果你要显式地转换,你可以使用 make_response() 函数然后再进行修改。
用法示例:
当访问’/’ URL时,会打开一个简单的表单index.html,可以设置UserID。
from flask import Flask, render_template, make_response
from</