Navigation

When you have to work with form data submitted by a browser view, code
quickly becomes very hard to read. There are libraries out there designed
to make this process easier to manage. One of them is WTForms which we
will handle here. If you find yourself in the situation of having many
forms, you might want to give it a try.

When you are working with WTForms you have to define your forms as classes
first. I recommend breaking up the application into multiple modules
(Larger Applications) for that and adding a separate module for the
forms.

Getting the most out of WTForms with an Extension

The Flask-WTF extension expands on this pattern and adds a
few little helpers that make working with forms and Flask more
fun. You can get it from PyPI.

@app.route('/register',methods=['GET','POST'])defregister():form=RegistrationForm(request.form)ifrequest.method=='POST'andform.validate():user=User(form.username.data,form.email.data,form.password.data)db_session.add(user)flash('Thanks for registering')returnredirect(url_for('login'))returnrender_template('register.html',form=form)

Notice we’re implying that the view is using SQLAlchemy here
(SQLAlchemy in Flask), but that’s not a requirement, of course. Adapt
the code as necessary.

Things to remember:

create the form from the request form value if
the data is submitted via the HTTP POST method and
args if the data is submitted as GET.

to validate the data, call the validate()
method, which will return True if the data validates, False
otherwise.

Now to the template side. When you pass the form to the templates, you can
easily render them there. Look at the following example template to see
how easy this is. WTForms does half the form generation for us already.
To make it even nicer, we can write a macro that renders a field with
label and a list of errors if there are any.

This macro accepts a couple of keyword arguments that are forwarded to
WTForm’s field function, which renders the field for us. The keyword
arguments will be inserted as HTML attributes. So, for example, you can
call render_field(form.username,class='username') to add a class to
the input element. Note that WTForms returns standard Python unicode
strings, so we have to tell Jinja2 that this data is already HTML-escaped
with the |safe filter.

Here is the register.html template for the function we used above, which
takes advantage of the _formhelpers.html template: