Flask WTForms HiddenInput 值未发送到应用程序

Flask WTForms HiddenInput value not being sent to app

所以,这是我在 Stack Overflow 上的第一个问题。请温柔点..

我正在尝试将 IntegerField 用作 HiddenInput,但是当我提交表单时,该字段的值没有发送到我的应用程序。 (当我调试时,该值为''),但我可以看到通过开发工具在页面本身上设置了正确的值。

如果我删除 widget=HiddenInput(),它可以正常工作。我敢肯定有一些非常明显的原因,但我一直找不到。

class:

class RemoveTimeslot(FlaskForm):
    ts_id = IntegerField(widget=HiddenInput())
    remove = SubmitField('Remove')

从观点来看:

slot = Timeslot.query.filter_by(id=rem_form.ts_id.data).first()
    if slot:
        db.session.delete(slot)
        db.session.commit()
        flash('Timeslot Removed!')
        return redirect(url_for('admin.timeslots'))
    else:
        flash('Failed to remove timeslot!', 'warning')

来自模板: '''

{% for slot in slots %}
<tr>
    <td>{{slot.start.isoformat(timespec='minutes')}}</td>
    <td>{{slot.end.isoformat(timespec='minutes')}}</td>
    <td>{{slot.duration}} Minutes</td>
    <td>
        <form method="POST" action="">
            {{ rem_form.hidden_tag() }}
            {{ rem_form.remove(class="btn btn-dark") }}
            {{ rem_form.ts_id(value=slot.id) }}
        </form>
    </td>
</tr>
{% endfor %}

如有任何帮助,我们将不胜感激!

我终于能够通过在模板中添加 type="hidden" 来让它工作。在 class 本身中,我将其保留为 ts_id = IntegerField(),但是当我在模板中创建字段时,我将其更改为 {{ rem_form.ts_id(value=slot.id, type="hidden") }},并且成功了!我不知道为什么一个与另一个的工作方式不同...如果您有任何想法,请告诉我!