python - 将表单数组发送到 Flask

标签 python html flask

我有一个带有多个输入的 HTML 表单,命名如下:

<input name="hello[]" type="text" />
<input name="hello[]" type="text" />
<input name="hello[]" type="text" />

PHP 中,您将其作为数组获取,但在 Python 中使用 Flask 的方式是否相同?

我试过这个:

hello = request.form['hello']

print(hello)

但这不起作用,我收到了 400 Bad Request:

Bad Request

The browser (or proxy) sent a request that this server could not understand.

我如何在 Flask 中做到这一点?

最佳答案

您正在关注 PHP convention of adding brackets to the field names .它不是 Web 标准,但因为 PHP 开箱即用地支持它,所以它很受欢迎; Ruby on Rails 也使用它。

如果您确实使用该约定,要在 Flask 端获取 POST 数据,您需要在字段名称中包含方括号。您可以使用 MultiDict.getlist() 检索列表的所有值:

hello = request.form.getlist('hello[]')

当然,您根本不必使用 [] 约定。不将 [] 附加到 hello 名称将完全正常,此时您将使用 request.form.getlist('hello') 在 Flask 中。

关于python - 将表单数组发送到 Flask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24808660/

相关文章:

javascript - jQuery on click 功能在 Android 移动浏览器中不起作用

python - 创建一个使用 Flask 和 Bootstrap 生成的可排序表(使用 React.js)

Python:动态运行文件夹中所有Python脚本中的所有函数

javascript - 在滚动 div 的边缘淡化子 flexbox 元素?

python - DJANGO:如何 list_display 反向外键属性?

将 jQuery 添加到我的页面后,它无法正常工作

flask - flask_dance : Cannot get OAuth token without an associated user

python - 由于 Web 部署而将绝对路径更改为相对路径(Python - Flask)

python - 使用字典时如何避免 KeyError?

python - 使用 SeleniumLibrary 中的 ScreenshotKeywords