WTForms 如何将 RadioField 渲染为 StringField

WTForms how to render RadioField as StringField

因为很难 dynamically change WTForm,我试图渲染 FieldList of RadioFields 就好像它是 FieldList of [=16] =].我的表格看起来像这样:

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()

但这根本就没有呈现。对此有何建议?

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

<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>

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