Redirect to other view after submitting form
You have the right goal: it's good to redirect after handling form data. Rather than returning render_template
again, use redirect
instead.
from flask import redirect, url_for, survey_id@app.route('/success/<int:result_id>')def success(result_id): # replace this with a query from whatever database you're using result = get_result_from_database(result_id) # access the result in the tempalte, for example {{ result.name }} return render_template('success.html', result=result)@app.route('/survey', methods=["GET", "POST"])def survey(): if request.method == 'POST': # replace this with an insert into whatever database you're using result = store_result_in_database(request.args) return redirect(url_for('success', result_id=result.id)) # don't need to test request.method == 'GET' return render_template('survey.html')
The redirect will be handled by the user's browser, and the new page at the new url will be loaded, rather than rendering a different template at the same url.