【问题标题】:wtforms SelectField with dynamic choices always returns "none" for data具有动态选择的 wtforms SelectField 始终为数据返回“无”
【发布时间】:2023-03-27 04:18:01
【问题描述】:

我是 wtforms 的新手。我必须向用户提供水果清单和每种水果的成本,如下所示,

水果的总数是动态生成的,每个水果的价格也是动态生成的。

以下是我的声明,

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 会动态填充列表,每个列表都有选项。

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

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。我还尝试在检索所选值之前显示所有选项,它正确显示了所有选项。几次我更改了代码,但它总是显示“无”值。有人可以告诉我可能是什么问题。

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

【问题讨论】:

    标签: python flask wtforms flask-wtforms wtforms-json


    【解决方案1】:

    我解决了这个问题!

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

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

    即,替换为以下语句

        arr.popentry()
        arr.append(value)
    

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

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

    - 斯拉万

    【讨论】:

      猜你喜欢
      • 2020-02-27
      • 2018-04-05
      • 2021-12-20
      • 2021-05-15
      • 2016-09-10
      • 2019-10-12
      • 1970-01-01
      • 2020-06-14
      • 2020-06-03
      相关资源
      最近更新 更多