Flask默认是GET请求
如果我们在一个页面中即需要GET请求又需要POST请求那么我们需要重写methods方法:
@H_502_21@<!DOCTYPE HTML><HTML lang="en"><head> <Meta charset="UTF-8"> <Title>Title</Title></head><body><form action="" method="post"> 用户名:<input type="text"> 密码:<input type="password"> <input type="submit" value="登录"></form></body></HTML> @H_502_21@from flask import Flask,render_templateapp = Flask(__name__)@app.route(‘/login‘,methods=["POST","GET"]) # 重写methods方法def login(): return render_template("login.HTML")if __name__ == ‘__main__‘: app.run("0.0.0.0",9876) 其他方法 Flask中的request为我们提供了很多属性,只需导入request直接调用对应的属性就可以直接看到效果 request.methodFlask 的 request 中给我们提供了一个 method 属性里面保存的就是前端的请求的方式
@H_502_21@from flask import Flask,render_template,requestapp = Flask(__name__)@app.route(‘/login‘,"GET"])def login(): # 获取前端的请求的方式 print(request.method) return render_template("login.HTML")if __name__ == ‘__main__‘: app.run("0.0.0.0",9876) request.form获取请求数据
@H_502_21@from flask import Flask,"GET"])def login(): if request.method == "GET": return render_template("login.HTML") if request.method == "POST": # 获取form表单提交的数据 print(request.form) # ImmutableMultIDict([(‘username‘,‘henry‘),(‘password‘,‘123456‘)]) return "200 ok"if __name__ == ‘__main__‘: app.run("0.0.0.0",9876) 通过reqeust.form属性做登录验证 @H_502_21@from flask import Flask,‘123456‘)]) username = request.form.get("username") password = request.form.get("password") if username == "henry" and password == "123456": return "200 ok" else: return "404"if __name__ == ‘__main__‘: app.run("0.0.0.0",9876)注意:ImmutableMultIDict类型的数据和我们python中的字典用法是一样的
我们可以通过to_dict()方法将上面的这种数据类型转换成我们python中的字典,如下所示
@H_502_21@from flask import Flask,"GET"])def login(): if request.method == "GET": return render_template("login.HTML") if request.method == "POST": # 获取form表单提交的数据 print(request.form.to_dict()) # {‘username‘: ‘henry‘,‘password‘: ‘123456‘} username = request.form.get("username") password = request.form.get("password") if username == "henry" and password == "123456": return "200 ok" else: return "404"if __name__ == ‘__main__‘: app.run("0.0.0.0",9876)我们用get方法取字典中的值防止报错
request.headers获取请求头中的数据
@H_502_21@print(request.headers)#Host: 192.168.16.42:9876#Connection: keep-alive#Content-Length: 27#Cache-Control: max-age=0#Origin: http://192.168.16.42:9876#upgrade-insecure-requests: 1#Content-Type: application/x-www-form-urlencoded#User-Agent: Mozilla/5.0 (windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/75.0.3770.100 Safari/537.36#Accept: text/HTML,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3#Referer: http://192.168.16.42:9876/login#Accept-EnCoding: gzip,deflate#Accept-Language: zh-CN,zh;q=0.9 request.args
获取GET请求中的url参数
@H_502_21@print(request.args)# ImmutableMultIDict([(‘name‘,(‘age‘,‘18‘)])# 通过to_dict()方法转换成字典print(request.args.to_dict())# {‘name‘: ‘henry‘,‘age‘: ‘18‘} request.files上传文件
前端页面 @H_502_21@<!DOCTYPE HTML><HTML lang="en"><head> <Meta charset="UTF-8"> <Title>Title</Title></head><body><form action="" method="post" enctype="multipart/form-data"> 用户名:<input type="text" name="username"> 密码:<input type="password" name="password"> <input type="file" name="my_file"> <input type="submit" value="登录"></form></body></HTML> 后端代码 @H_502_21@from flask import Flask,"GET"])def login(): if request.method == "GET": return render_template("login.HTML") if request.method == "POST": username = request.form.get("username") password = request.form.get("password") if username == "henry" and password == "123456": print(request.files) # ImmutableMultIDict([(‘my_file‘,<fileStorage: ‘click点击事件.gif‘ (‘image/gif‘)>)]) files = request.files.get("my_file") # files是fileStorage类型 注意:不是文件句柄 print(files.filename) # click点击事件.gif 【文件名字】 files.save(files.filename) # fileStorage类型调用save方法保存 return "200 ok" else: return "404"if __name__ == ‘__main__‘: app.run("0.0.0.0",9876) fp = os.path.join( "templates" ,files.filename) # 通过os模块指定保存路径 |
以上是内存溢出为你收集整理的【Flask】Request全部内容,希望文章能够帮你解决【Flask】Request所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)