【问题标题】:WTForms how to render RadioField as StringFieldWTForms 如何将 RadioField 呈现为 StringField
【发布时间】:2022-01-01 11:11:09
【问题描述】:

由于dynamically change WTForm 非常困难,我尝试将RadioFieldsFieldList 渲染为就像StringFieldsFieldList 一样。我的表单看起来像这样:

class SingleForm(FlaskForm):

    radio = RadioField()


class EntireForm(FlaskForm):

    # fieldlist of singleforms
    field_forms = FieldList(FormField(SingleForm))

我目前正在渲染我的代码(普通单选按钮)

<table>
    <tr>
        {% for single_form in form.field_forms %}
            <td>
                {{ single_form.form.radio(required='required') }}
            </td>
        {% endfor %}
    </tr>
</table>

基于this answer,我尝试在渲染之前更改routes.py 中表单的小部件类型,如下所示:

# this actually adds some SingleForm RadioFields to the FieldList...
some_amount_of_fields = 5
for idx in range(some_amount_of_fields):
    form.field_forms.append_entry({})

# now change their widget type...
for single_form in form.field_forms:
    single_form.form.widget = HiddenInput()

但这根本没有渲染。对此有何建议?

【问题讨论】:

    标签: python flask-wtforms wtforms


    【解决方案1】:

    对于后来遇到这个问题的人,我使用 Jinja 纯粹在 HTML 中解决了这个问题:

    <form>
        {% for single_form in form.field_form %}
            <input type="text" id="{{ single_form.form.radio.label.field_id }}" name="{{single_form.form.radio.name }}" required='required')>
        {% endfor %}
    </form>
    

    这会自动填写原始表格,因此无需手动插入。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-05
      相关资源
      最近更新 更多