Post/Redirect/Get pattern in flask

nalzok

The view function of my toy app was:

@app.route('/', methods=['GET', 'POST'])
def index():
    name = None
    form = NameForm()
    if form.validate_on_submit():
        name = form.name.data
        form.name.data = ''
    return render_template('index.html', form=form, name=name)

And it looks like this when I use PRG:

@app.route('/', methods=['GET', 'POST'])
def index():
    form = NameForm()
    if form.validate_on_submit():
        session['name'] = form.name.data
        return redirect(url_for('index'))
    return render_template('index.html', form=form, name=session.get('name'))

As you can see, the form.name.data = '' line is used to clear the input field in the first version, but it's not needed in the second version. I thought Flask-WTF would automatically pass the text in StringField into the new form instance, but for some reasons, it didn't.

My question is: Why form.name.data is no longer available between different requests when I use PRG?

Daniel Roseman

It can't pass anything on a redirect, as it is a completely new request.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related