htmlformsflaskflask-sqlalchemyflask-wtforms

Flask WTForms HiddenInput value not being sent to app


So, this is my first ever question on Stack Overflow. Please be gentile..

I'm trying to use an IntegerField as a HiddenInput, but when I submit the form, the value of the field doesn't get sent to my app. (When I debug, the value is ''), but I can see that the proper value is set on the page itself via dev tools.

If I remove the widget=HiddenInput(), it works without issue. I'm sure there's some ridiculously obvious reason why, but I've been unable to find it.

The class:

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

From the view:

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

From the template: '''

{% 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 %}

Any help would be appreciated!


Solution

  • I was finally able to get it to work by adding type="hidden" in the template. In the class itself, I left it as ts_id = IntegerField(), but when I created the field in the template I changed it to {{ rem_form.ts_id(value=slot.id, type="hidden") }}, and that worked! I don't know why one works differently than the other... let me know if you have any ideas!