发送表单数组到Flask [英] Sending a form array to Flask
问题描述
< input name =hello []类型=text/>
< input name =hello []type =text/>
< input name =hello []type =text/>
在PHP中,您将它作为一个数组来使用,但是与使用Flask的Python相同吗?
$ b
hello = request.form ['hello' ]
打印hello
在Flask中,但没有成功,我得到了一个400不良要求:
错误请求
浏览器(或代理)发送请求,无法理解。
如何在Flask中执行此操作?
您正在关注在字段名称中添加括号的PHP惯例。这不是一个网络标准,但是因为PHP支持开箱即用, Ruby on Rails也使用它。
如果你使用这个约定,要在Flask端获得POST数据,你需要包含 方括号中的字段名称。您可以使用检索列表的所有值 MultiDict.getlist()
:
hello =请求.form.getlist('hello []')
您不必使用<$当然是c $ c> [] convention。不要将 []
添加到 hello
名称中,这样可以很好地工作,在这一点上,您可以使用 request.form.getlist('hello')
在Flask中。
I have a html form with multiple inputs named like this:
<input name="hello[]" type="text" />
<input name="hello[]" type="text" />
<input name="hello[]" type="text" />
In PHP you get this as an array but is it the same way in Python using Flask?
I tried this:
hello = request.form['hello']
print hello
In Flask, but that did not work, I got a "400 bad request":
Bad Request
The browser (or proxy) sent a request that this server could not understand.
How do I do it in Flask?
You are following a PHP convention of adding brackets to the field names. It's not a web standard, but because PHP supports it out of the box it is popular; Ruby on Rails also uses it.
If you do use that convention, to get the POST data on the Flask side you need to include the square brackets in the field name. You can retrieve all values of the list using MultiDict.getlist()
:
hello = request.form.getlist('hello[]')
You don't have to use the []
convention at all, of course. Not appending the []
to the hello
name will work perfectly fine, at which point you'd use request.form.getlist('hello')
in Flask.
这篇关于发送表单数组到Flask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!