[英]Wtforms - How FieldList receives input from frontend
I am curious how FieldList
of wtforms works, I did some experiment with the front end using javascript. 我很好奇FieldList
的FieldList如何工作,我使用javascript在前端做了一些实验。
So basically I have a multiple repeatable fields, and there is a button to dynamically add new field and changes its id
, name
, for
following the wtforms convention (eg transaction-item-0-something
for first field, transaction-item-1-something
for second field, etc). 所以基本上我有多个可重复的字段,并且有一个按钮可以动态添加新字段并更改其id
, name
, for
遵循wtforms约定(例如,第一个字段的transaction-item-0-something
, transaction-item-1-something
第二场的transaction-item-1-something
,等等)。
One experiment that I did was putting same id
and name
same for 2 of field list (eg transaction-item-0-something
for 1st field, transaction-item-0-something
for 2nd field). 一个实验,我并是把相同的id
和name
相同的字段列表2(例如transaction-item-0-something
的1场, transaction-item-0-something
的第二场)。 and wtforms only took the first input value (is expected). 而wtforms仅采用第一个输入值(预期)。
Second experiment was the one that bugs me, I deliberately changed some of the number so it would skip some number ( transaction-item-0-something
for 1st field, transaction-item-2-something
for 2nd field), and it worked fine, wtforms received both of the input. 第二个实验是一个困扰我的实验,我故意更改了一些数字,因此它会跳过一些数字(第一个字段的transaction-item-0-something
,第二个字段的transaction-item-2-something
),并且工作正常,wtforms接收了两个输入。
So this makes me wondering, do id
and name
not need to be ordered? 所以这让我想知道,是否不需要订购id
和name
? if so how the wtforms handle the received input then, is just getting the all the ids of item which contains transaction-item-x-something
? 如果是这样,wtforms如何处理接收到的输入,那么,是否只是获取包含transaction-item-x-something
的item的所有id? and just loop through it but also check if the registered id is in there? 并循环浏览它,还要检查注册的ID是否在其中?
Actually, I just dug through the source code and found out the order does not matter, as long as it is a number ( .isdigit()
), it will loop through all the received data and then put it inside a set
which is like python keys
in dictionary
, so duplicate value will be removed. 其实,我只是通过挖源代码 ,并发现了顺序并不重要,只要它是一个数字( .isdigit()
它会遍历所有接收到的数据,然后把它放在里面set
这就好比dictionary
python keys
,因此重复值将被删除。
Which means html will send every <input>
regardless of its id
or name
is same and let the backend handles it 这意味着html会发送每个<input>
而不管其id
或name
是否相同,并由后端处理
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.