Flask WTForms how to prevent duplicate form submission

Question:

I’m new to Flask.

Forms.py:

class NoteForm(FlaskForm):
    note = fields.TextAreaField("Note")
    add_note = fields.SubmitField("Add Note")

router.py:

add_note_form = forms.NoteForm()

template:

<div class="form-group">
  {{ add_note_form.add_note}}
</div>

Now if I click the add note button multiple times in a very short time ,the form will be summitted for multiple times, especial when the page is loading slowly.

Is there anyway I can prevent duplicate form submission ?

Asked By: William

||

Answers:

One way to do this is to disable the submit button after form is submitted

onClick="this.form.submit(); this.disabled=true; this.value='Saving…'; "

Another way would be to give you record an id and check for duplicate in the backend.

Answered By: Elie Saad