Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7909225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T12:19:17+00:00 2026-06-03T12:19:17+00:00

I use WTForms and Flask with Flask-WTF extension My form looks like: class CommentForm(Form):

  • 0

I use WTForms and Flask with Flask-WTF extension

My form looks like:

class CommentForm(Form):
    body = TextAreaField('Body', [validators.Length(min=4, max=300)])
    entity_id = HiddenField('Entity ID', [validators.required()])

Jinja2 Template:

 <form method="POST" action="{{ request.url }}#comment-question" id="comment-question">
     <div>{{ comment_form.body }} <button type="submit">Submit</button></div>
     {{ comment_form.entity_id(value=question.id) }}
     {{ comment_form.hidden_tag() }}
 </form>

Rendered form:

<form method="POST" action="http://localhost:5000/answers/1/question-0#comment-question" id="comment-question">
  <div><textarea id="body" name="body"></textarea> <button type="submit">Submit</button></div>
  <input id="entity_id" name="entity_id" type="hidden" value="1">
  <div style="display:none;"><input id="csrf_token" name="csrf_token" type="hidden" value="20120507081937##ee73cc3cfc053266fef78b48cc645cbf90e8fba6"><input id="entity_id" name="entity_id" type="hidden" value=""></div>
</form>

Is it possible to prevent the double form submit on the browser refresh button click without changing the form “action” and doing redirects?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T12:19:22+00:00Added an answer on June 3, 2026 at 12:19 pm

    I don’t have too much experience using WTForms or Flask, but Django class-based views prevent double-posts by redirecting after a POST, so I had assumed performing a redirect is the way to go for this sort of thing.

    One alternative is to generate a unique token and attach it to your form parameters (much like a CSRF token). Cache this value and check against it on form submission. A rather primitive example for Django can be found here.

    Edit: Sample code

    Although I would really just go with performing a redirect after a successful form submission, here’s an example of generating a form token which borrows heavily from this Flask snippet on CSRF protection:

    # yourapp/views/filters.py
    
    import random
    from string import ascii_letters, digits
    
    from flask import request, session, redirect
    from yourapp import app
    
    
    def generate_form_token():
        """Sets a token to prevent double posts."""
        if '_form_token' not in session:
            form_token = \
                ''.join([random.choice(ascii_letters+digits) for i in range(32)])
            session['_form_token'] = form_token
        return session['_form_token']
    
    
    @app.before_request
    def check_form_token():
        """Checks for a valid form token in POST requests."""
        if request.method == 'POST':
            token = session.pop('_form_token', None)
            if not token or token != request.form.get('_form_token'):
                redirect(request.url)
    
    
    app.jinja_env.globals['form_token'] = generate_form_token
    

    And in your template:

    <!-- Again, I've never used WTForms so I'm not sure if this would change when using that app. -->
    <input type='hidden' name='_form_token' value='{{ form_token() }}' />
    

    Note that using the CSRF protection method in the snippet also accomplishes pretty much the same effect (although the above code performs a redirect, while the snippet returns a 403).

    But this really begs the question–if you’re performing a redirect on an invalid token, why not get rid of all this complexity and just redirect on successful form submission?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use WTForms with Flask via the Flask.WTF extension. This question isn't Flask-specific, though.
I want to re-use a template I have with my WTForms form: <th>${form.name.label}</th> <td>${form.name()}</td>
I have a form with flask-wtf for uploading images, also file field can be
Use case: Wanna insert custom annotation to fields in java class generated by JAXB
Use case : User fills out a form to add a product and register
use strict; seems awesome, and we'd really like to use it at our shop.
Use Html element SELECT in aspx page like: <SELECT id=MySelection name=MySelection runat=server DataValueField=ID DataTextField=Name></SELECT>
Use trap to capture signals like this: i=-1;while((++i<33)); do trap echo $i >> log.txt
Use paperclip for my images. Models: class Country < ActiveRecord::Base has_many :regions has_many :assets,
I use AppEngine to host an Eclipse update site. I would like to keep

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.