If any error is encountered while processing the form data, redirect to the home
endpoint with the POST data (using code 307).
@app.route('/edit_profile', methods=['GET', 'POST'])
def edit_profile():
ep_form = EditProfile()
if ep_form.validate_on_submit():
# If the data is validated and good
# In here is the code that handles the new profile data
return render_template('edit_html', ep_form=ep_form)
else:
# If any errors are encountered, redirect
# back to the home endpoint along with POST data
# using code 307
return redirect(url_for('home'), code=307)
Now in the home
endpoint, we need to handle the POST data that might get redirected from edit_profile
.
@app.route('/', methods=['GET','POST'])
def home():
# fetch data from DB, other values
ep_form = EditProfile()
# We need to call validate_on_submit so that
# the data is validated and errors are populated
if request.method == "POST":
ep_form.validate_on_submit()
return render_template('home.html', some_var=some_var, ep_form=ep_form)
In this way, home view function will have access to the form data, validate it and can show the errors.