当前位置: 首页 > ops >正文

Flask——request的form_data_args用法

request中包含了前端发送过来的所有请求数据,在使用前要进行导入request

from flask import Flask,request

1.formdata是用来提取请求体数据,通过request.form可以直接提取请求体中的表单格式的数据,是一个类字典的对象,例如:

from flask import Flask,requestapp = Flask(__name__)@app.route("/index",methods=["GET","POST"])
def index():# request中包含了前端发送过来的所有请求数据# form和data是用来提取请求体数据# 通过request.form可以直接提取请求体中的表单格式的数据,是一个类字典的对象name = request.form.get("name")age = request.form.get("age")return "hello world name=%s ,age=%s"%(name, age)if __name__ == '__main__':app.run(host="127.0.0.1", port=8000,debug=True)

但是通过get方法只能拿到多个同名参数的第一个,getlist方法可以拿到多个同名参数的列表,例如:

from flask import Flask,requestapp = Flask(__name__)@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)# getlist方法可以拿到多个同名参数的列表name_li = request.form.getlist("name")return "hello world name=%s ,age=%s, name_li=%s"%(name, age, name_li)if __name__ == '__main__':app.run(host="127.0.0.1", port=8000,debug=True)

2.若数据不是表单格式的数据,可用requset.data来进行提取

from flask import Flask,requestapp = Flask(__name__)@app.route("/index",methods=["GET","POST"])
def index():result= request.datareturn resultif __name__ == '__main__':app.run(host="127.0.0.1", port=8000,debug=True)

3.args是用来提取url中的参数,例如http://127.0.0.1:8000/index?city=BeiJing 查询字符串,可以用args方式进行提取city参数,范例如下:

from flask import Flask,requestapp = Flask(__name__)# http://127.0.0.1:8000/index?city=huizhou 查询字符串 可以用args方式进行提取
@app.route("/index",methods=["GET","POST"])
def index():# args是用来提取url中的参数city = request.args.get("city")day = request.args.get("day")return "hello world city=%s, day=%s"%( city, day)if __name__ == '__main__':app.run(host="127.0.0.1", port=8000,debug=True)

request的form_data_args用法就先写到这里,读者有什么疑问可以评论或私信,博主看到会进行解答的。

http://www.xdnf.cn/news/11151.html

相关文章:

  • 汇编语言入门教程
  • HttpWatch使用教程
  • 万用表之电压测量原理(基于ICL7107)
  • 多点定位MLAT系统解决方案
  • static 函数和普通函数
  • 归并排序(Merge Sort)
  • C++ assert.h头文件
  • H264解码之TS流解析
  • VRML基础知识
  • SCJP 学习笔记
  • 数字货币/币币交易所系统开发|案例介绍|APP开发|项目测试
  • 图片Base64编码
  • xiao77论坛php,论坛
  • 缓存服务器
  • MATLAB中的代数环概念
  • Centos 6.3 安装 yozo office (永中office)
  • window.open()各参数详解
  • infopath java包_InfoPath 2013 修补程序包 (Ipeditor x none.msp) 的描述︰ 2014 年 4 月 8,...
  • win10 如何卸载OfficeScan
  • Altium Designer 10 介绍、原理图及其模板常规设计
  • 几个很特别的音乐搜索网站
  • C#中的Event的使用
  • 大数据分析的主流技术:比较与评估
  • SEO初级|网站结构优化—301重定向
  • 什么是计算机病毒?是怎么产生的?
  • winpcap简介
  • Linu基本知识(二)——Linux系统以及相关命令
  • Python中range函数的使用方法
  • layer-list -- layer-list的基本使用介绍
  • ARM 架构(V7,V8),和ARM内核区别,从ARM7,ARM9到Cortex-A7,A8,A9,A12,A15到Cortex-A53,A57