Search code examples
pythonflaskpost-redirect-get

How to rewrite this Flask view function to follow the post/redirect/get pattern?


I have a small log browser. It retrieves and displays a list of previously logged records depending on user's input. It does not update anything.

The code is very simple and is working fine. This is a simplified version:

@app.route('/log', methods=['GET', 'POST'])
def log():
    form = LogForm()
    if form.validate_on_submit():
        args = parse(form)
        return render_template('log.html', form=form, log=getlog(*args))
    return render_template('log.html', form=form)

However it does not follow the post/redirect/get pattern and I want to fix this.

Where should I store the posted data (i.e. the args) between post and get? What is the standard or recommended approach? Should I set a cookie? Should I use flask.session object, create a cache there? Could you please point me in the right direction? Most of the time I'm writing backends...


UPDATE:

I'm posting the resulting code.

@app.route('/log', methods=['POST'])
def log_post():
    form = LogForm()
    if form.validate_on_submit():
        session['logformdata'] = form.data
        return redirect(url_for('log'))
    # either flash errors here or display them in the template
    return render_template('log.html', form=form)

@app.route('/log', methods=['GET'])
def log():
    try:
        formdata = session.pop('logformdata')
    except KeyError:
        return render_template('log.html', form=LogForm())

    args = parse(formdata)
    log = getlog(args)
    return render_template('log.html', form=LogForm(data=formdata), log=log)

Solution

  • So, ultimately the post/redirect/get pattern protects against submitting form data more than once. Since your POST here is not actually making any database changes the approach you're using seems fine. Typically in the pattern the POST makes a change to underlying data structure (e.g. UPDATE/INSERT/DELETE), then on the redirect you query the updated data (SELECT) so typically you don't need to "store" anything in between the redirect and get.

    With all the being said my approach for this would be to use the Flask session object, which is a cookie that Flask manages for you. You could do something like this:

    @app.route('/log', methods=['GET', 'POST'])
    def log():
        form = LogForm()
        if form.validate_on_submit():
            args = parse(form)
            session['log'] = getlog(*args)
            return redirect(url_for('log'))
        saved = session.pop('log', None)
        return render_template('log.html', form=form, log=saved)
    

    Also, to use session, you must have a secret_key set as part of you application configuration.

    Flask Session API

    UPDATE 1/9/16

    Per ThiefMaster's comment, re-arranged the order of logic here to allow use of WTForms validation methods for invalid form submissions so invalid form submissions are not lost.