具有动态选择的wtforms SelectField始终返回"none"(无).用于数据 [英] wtforms SelectField with dynamic choices always returns "none" for data

查看:73
本文介绍了具有动态选择的wtforms SelectField始终返回"none"(无).用于数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是wtforms的新手.我必须向用户提供水果列表以及每种水果的成本,如下所示,

I am new to wtforms. I have to provide user with list of fruits and cost of each fruit as shown below,

动态生成水果总数,并且动态生成每个水果价格.

Total number of fruits are dynamically generated and each fruit prices is also dynamically generated.

下面是我的声明,

from flask.ext.wtf import Form 
class SelectForm(Form):
    def __init__(self, csrf_enabled=False, *args, **kwargs):
        super(SelectForm, self).__init__(csrf_enabled=csrf_enabled, *args, **kwargs)
    fruits_list = wtforms.FieldList(
            wtforms.SelectField('fruits',
                validators = [validators.Optional()]
                ),
            )
fruits_labels = wtforms.RadioField('Fruit',
            choices = [],
            )

模板包含以下代码:

{% for fruit in form.fruits_labels %}
<tr>
    <td>
        {{fruit}}
        {{fruit.label}}
    </td>
    <td>
        {% set fruitslist = form.fruits_list[loop.index0] %}
        {% if fruitslist.choices|length %}
        {{fruitslist(class='form-control')}}
        {% endif %}
    </td>
</tr>
{% endfor %}

在呈现模板之前,fruits_labels会动态填充选项和form.fruits_list会动态填充列表,每个列表都有选项.

Before rendering the template, fruits_labels is dynamically populated with choices and form.fruits_list is dynamically populated with the lists, each list having choices.

用户可以选择带有价格的任何特定水果,而其余所有其他选择输入将是可选的,然后他可以提交表格.提交表单后,fruits_labels会动态填充选项,而form.fruits_list会动态填充列表,每个列表(在验证之前)都有选择,如下所示.

User can select any particular fruit with a price, and remaining all other select inputs will be optional and then he can submit the form. After submitting the form, fruits_labels is dynamically populated with choices and form.fruits_list is dynamically populated with the lists, each list having choices (before validating) as shown below.

populate_fruits_list()  #form.fruits_list is dynamically populated in this function
if not form.validate_on_submit():
    return render_template('new.html', form=form)

i=0
while i<len(form.fruits_list):
    print 'form.fruits_list choices[',i,']: ', form.fruits_list[i].data
    i=i+1

print 'selection: ', form.fruits_list[userselection].data    # userselection is a variable that contains the index of the fruit user has selected.

以下是输出:

form.fruits_list choices[ 0 ]:  [('0', '-select-'), (1, '1')]
form.fruits_list choices[ 1 ]:  [('0', '-select-'), (30, '30'), (17, '17'), (16, '16'), (15, '15'), (14, '14'), (7, '7'), (6, '6'), (5, '5'), (4, '4'), (3, '3'), (2, '2'), (1, '1')] 
form.fruits_list choices[ 2 ]:  [('0', '-select-'), (30, '30'), (29, '29'), (28, '28'), (19, '19'), (18, '18'), (13, '13'),  (3, '3'), (2, '2'), (1, '1')] 
form.fruits_list choices[ 3 ]:  [('0', '-select-'), (30, '30'), (29, '29'), (28, '28'),  (21, '21'), (20, '20'),  (12, '12'), (11, '11'), (10, '10'),  (2, '2'), (1, '1')] 
selection: None

即使我选择了一个值为30的fruit3,我也不知道为什么所选的值不显示为none.我也尝试在检索所选值之前显示所有选择,它正确显示了所有选择.我几次更改了代码,但始终显示无"值.有人可以让我知道可能是什么问题.

Even though I have selected a fruit3 with value 30, I don't know why the selected value is displayed as none. Also I tried displaying all the choices before retrieving the selected value, it displayed all the choices correctly. Several times I changed the code, but it always displays "none" value. Could someone please let me know what may be the issue.

如果您能提供一些示例,那将非常有帮助.感谢您的时间和帮助.

It would be really helpful if you can provide me some example. Thanks for your time and help.

推荐答案

我解决了该问题!

用户提交表单后,我可以正确接收提交的值,但是在populate_fruits_list()方法中,我可以通过使用pop_entry()从列表中删除元素来使列表为空.列表为空后,我将元素再次添加到列表中.由于删除了列表元素,因此用户对此字段的选择将重置为无".

After user submits the form, I am receiving the submitted values properly, but in populate_fruits_list() method, I am making the list empty by removing the elements from the list, using pop_entry(). Once the list is empty, I am adding the elements to the list again. Because of the deletion of list elements, user selection to this field is resetting to "None".

解决方案:提交表单后,如果有任何字段是动态填充的,我们不应该从列表中删除条目,而是可以使用arr [0] = value这样的索引来重新分配值.

Solution: After the form was submitted, if there are any fields which are populated dynamically, we shouldn't delete the entries from the list instead we can reassign the values using index like arr[0] = value.

即替换为以下语句

    arr.popentry()
    arr.append(value)

使用

    arr[i] = value  //where i is an index

希望这些信息对其他人有帮助.

Hope this information will be helpful to others.

-斯拉文

这篇关于具有动态选择的wtforms SelectField始终返回"none"(无).用于数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆