Skip to content
Myr

fixed some punctuation, fixed a few errors, in service of readability

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Zev Averbach · committed by Zev Averbach · parent 595fb7e · Browse files · 1 file+12 added−12 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+12 added−12 removed

  • other:+12 added−12 removed

1 file+12 added−12 removed

modified:docs/patterns/wtforms.rst+12 added-12 removed
1Form Validation with WTForms2============================34When you have to work with form data submitted by a browser view code4When you have to work with form data submitted by a browser view, code5quickly becomes very hard to read. There are libraries out there designed6to make this process easier to manage. One of them is `WTForms`_ which we7will handle here. If you find yourself in the situation of having many8forms, you might want to give it a try.
2 unmodified lines
11first. I recommend breaking up the application into multiple modules12(:ref:`larger-applications`) for that and adding a separate module for the13forms.1415.. admonition:: Getting most of WTForms with an Extension15.. admonition:: Getting the most out of WTForms with an Extension1617 The `Flask-WTF`_ extension expands on this pattern and adds a few18 handful little helpers that make working with forms and Flask more17 The `Flask-WTF`_ extension expands on this pattern and adds a18 few little helpers that make working with forms and Flask more19 fun. You can get it from `PyPI20 <https://pypi.python.org/pypi/Flask-WTF>`_.2122.. _Flask-WTF: http://pythonhosted.org/Flask-WTF/
30 unmodified lines
53 flash('Thanks for registering')54 return redirect(url_for('login'))55 return render_template('register.html', form=form)5657Notice that we are implying that the view is using SQLAlchemy here58(:ref:`sqlalchemy-pattern`) but this is no requirement of course. Adapt57Notice we're implying that the view is using SQLAlchemy here58(:ref:`sqlalchemy-pattern`), but that's not a requirement, of course. Adapt59the code as necessary.6061Things to remember:62631. create the form from the request :attr:`~flask.request.form` value if64 the data is submitted via the HTTP ``POST`` method and65 :attr:`~flask.request.args` if the data is submitted as ``GET``.662. to validate the data, call the :func:`~wtforms.form.Form.validate`67 method which will return ``True`` if the data validates, ``False``67 method, which will return ``True`` if the data validates, ``False``68 otherwise.693. to access individual values from the form, access `form.<NAME>.data`.7071Forms in Templates72------------------7374Now to the template side. When you pass the form to the templates you can74Now to the template side. When you pass the form to the templates, you can75easily render them there. Look at the following example template to see76how easy this is. WTForms does half the form generation for us already.77To make it even nicer, we can write a macro that renders a field with78label and a list of errors if there are any.
15 unmodified lines
94 </dd>95 {% endmacro %}9697This macro accepts a couple of keyword arguments that are forwarded to98WTForm's field function that renders the field for us. The keyword99arguments will be inserted as HTML attributes. So for example you can98WTForm's field function, which renders the field for us. The keyword99arguments will be inserted as HTML attributes. So, for example, you can100call ``render_field(form.username, class='username')`` to add a class to101the input element. Note that WTForms returns standard Python unicode102strings, so we have to tell Jinja2 that this data is already HTML escaped102strings, so we have to tell Jinja2 that this data is already HTML-escaped103with the ``|safe`` filter.104105Here the :file:`register.html` template for the function we used above which105Here is the :file:`register.html` template for the function we used above, which106takes advantage of the :file:`_formhelpers.html` template:107108.. sourcecode:: html+jinja109
17 unmodified lines