Flask获取请求参数
2020-09-14 本文已影响0人
测试探索
导包
from flask import request
就是flask中表示当前请求的request对象,request对象中保存了一次HTTP请求的一切信息

from flask import Flask,request
app = Flask(__name__)
# 127.0.0.1:5000/index?city=shenzhen&country=china 查询字符串 QueryString
@app.route("/index",methods=["GET","POST"])
def index():
#从request中包含了前端发送过来的所有请求信息
#form和data是用来提取请求体数据
#通过request.form可以直接提取请求体中的表单格式的数据,是一个类字典的对象
#通过get方法只能拿到多个同名参数的第一个
name = request.form.get("name")
age = request.form.get("age")
print(request.data)
city = request.args.get("city")
#args是用来提取url中的参数(查询字符串)
return "hello name=%s ,age=%s,city=%s" %(name,age,city)
if __name__ == '__main__':
app.run(debug=True)
查看form,data,args的取值方式

通过request.form.getlist("key")拿到同名参数列表
from flask import Flask,request
app = Flask(__name__)
# 127.0.0.1:5000/index?city=shenzhen&country=china 查询字符串 QueryString
@app.route("/index",methods=["GET","POST"])
def index():
#从request中包含了前端发送过来的所有请求信息
#form和data是用来提取请求体数据
#通过request.form可以直接提取请求体中的表单格式的数据,是一个类字典的对象
#通过get方法只能拿到多个同名参数的第一个
name = request.form.get("name")
age = request.form.get("age")
name_li = request.form.getlist("name")
print(request.data)
city = request.args.get("city")
#args是用来提取url中的参数(查询字符串)
return "hello name=%s ,age=%s,city=%s,name_li=%s" %(name,age,city,name_li)
if __name__ == '__main__':
app.run(debug=True)
