Skip to content Skip to sidebar Skip to footer

What Is The Technique Of Presenting Again Form Which Contains Error?

Using Flask + Flask-Classy + WTForms, I am trying to implement a route, such that, when user having error in their input form, application will presented again that form to the use

Solution 1:

I'm adding my answer to expand swdev's answer for anyone that doesn't understand why he can just render the template and show the errors.

Each field in a form contains a errors attribute that will be populated after validation (after form.validate()). These errors will be contained in the form object. So you can say something like,

{% if field.errors %}
    <span class="error help-inline">{{ field.errors|join(', ') }}</span>
{% endif %}

example source file. This is very nifty because you can just say

return render_template('users/form.html', form=form) 

and if it's the first render get a error free form and display errors only on the second rendering of the form.

I'm not sure why Swdev attempt to redirect the form caused it to be manipulated right now. I'm looking through the Werkzeug code right now to see if I can find an answer.

Solution 2:

Ow, how stupid I am. Just after I got a rest for a while, I found a simple answer, that is, instead of passing to another view method :

else:                                                                                                                                                                                                                     
   return redirect(url_for('UserView:validation_error', error_form=form))

I can easily render the template :

return render_template('users/form.html', form=form) 

That simple ;)

Post a Comment for "What Is The Technique Of Presenting Again Form Which Contains Error?"