Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clarification in docs of validate_on_sumbit() and csrf_token #567

Open
Jeroendevr opened this issue May 31, 2023 · 0 comments
Open

clarification in docs of validate_on_sumbit() and csrf_token #567

Jeroendevr opened this issue May 31, 2023 · 0 comments
Labels

Comments

@Jeroendevr
Copy link

Hi All,

Being new to flask-wtf, I've some question/suggestion regarding the docs.

I wanted to implement a form with a submit button. After pressing the button some function needs to be executed. This was already a challenge to me. Searching StackOverflow the validate_on_submit was frequently mentioned as a method to put in my view function.

After some tweaking I have found out that putting {{ form.csrf_token }} is required in order to work with validate_on_submit. However this was not something clear from the documentation for me.

Suggestion

Perhaps in the validate_on_submit docs add the following

In order to validate on submit, you need to provide the csrf_token within template containing the form.

Like to recieve your feedback and thoughts.

Example app

# app.py
from flask import Flask, render_template, redirect, url_for
from flask_wtf import FlaskForm
from wtforms import SubmitField

app = Flask(__name__)
app.config['SECRET_KEY'] = 'your-secret-key'

class MyForm(FlaskForm):
    submit = SubmitField('Submit')

@app.route('/', methods=['GET', 'POST'])
def index():
    form = MyForm()

    if form.validate_on_submit():
        # Perform some action upon form submission
        return redirect(url_for('success'))

    return render_template('index.html.jinja', form=form)

@app.route('/success')
def success():
    return 'Form submitted successfully!'
{# index.html.jinja #}
<!DOCTYPE html>
<html>
<head>
    <title>Submit Field Example</title>
</head>
<body>
    <h1>Submit Field Example</h1>
    <form method="POST" action="/">
        {{ form.csrf_token }}
        {{ form.submit() }}
    </form>
</body>
</html>
@azmeuk azmeuk added docs and removed docs labels Jul 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants