我有一个带有多个输入的 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/