Merge pull request #1520 from zevav/patch-2
Before adoption: this commit came in with the history the project adopted, so no landing records it.
fixed some punctuation, fixed a few errors, in service of readability
authored by Markus Unterwaditzer · committed by Markus Unterwaditzer · parents b528b2f, 5dfe918 · 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
Provenance
- Commit
ac12e36- Tree
0b15eec- Author
Markus Unterwaditzer <markus@unterwaditzer.net>- Committer
Markus Unterwaditzer <markus@unterwaditzer.net>- Parent 1
b528b2f- Parent 2
5dfe918
1 file changed+12 added−12 removed
modified:docs/patterns/wtforms.rst+12 added-12 removed
1 Form Validation with WTForms2 ============================3 4-When you have to work with form data submitted by a browser view code4+When you have to work with form data submitted by a browser view, code5 quickly becomes very hard to read. There are libraries out there designed6 to make this process easier to manage. One of them is `WTForms`_ which we7 will handle here. If you find yourself in the situation of having many8 forms, you might want to give it a try.2 unmodified lines11 first. I recommend breaking up the application into multiple modules12 (:ref:`larger-applications`) for that and adding a separate module for the13 forms.14 15-.. admonition:: Getting most of WTForms with an Extension15+.. admonition:: Getting the most out of WTForms with an Extension16 17- 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>`_.21 22 .. _Flask-WTF: http://pythonhosted.org/Flask-WTF/30 unmodified lines53 flash('Thanks for registering')54 return redirect(url_for('login'))55 return render_template('register.html', form=form)56 57-Notice that we are implying that the view is using SQLAlchemy here58-(:ref:`sqlalchemy-pattern`) but this is no requirement of course. Adapt57+Notice we're implying that the view is using SQLAlchemy here58+(:ref:`sqlalchemy-pattern`), but that's not a requirement, of course. Adapt59 the code as necessary.60 61 Things to remember:62 63 1. 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``.66 2. 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.69 3. to access individual values from the form, access `form.<NAME>.data`.70 71 Forms in Templates72 ------------------73 74-Now to the template side. When you pass the form to the templates you can74+Now to the template side. When you pass the form to the templates, you can75 easily render them there. Look at the following example template to see76 how easy this is. WTForms does half the form generation for us already.77 To make it even nicer, we can write a macro that renders a field with78 label and a list of errors if there are any.15 unmodified lines94 </dd>95 {% endmacro %}96 97 This macro accepts a couple of keyword arguments that are forwarded to98-WTForm's field function that renders the field for us. The keyword99-arguments will be inserted as HTML attributes. So for example you can98+WTForm's field function, which renders the field for us. The keyword99+arguments will be inserted as HTML attributes. So, for example, you can100 call ``render_field(form.username, class='username')`` to add a class to101 the input element. Note that WTForms returns standard Python unicode102-strings, so we have to tell Jinja2 that this data is already HTML escaped102+strings, so we have to tell Jinja2 that this data is already HTML-escaped103 with the ``|safe`` filter.104 105-Here the :file:`register.html` template for the function we used above which105+Here is the :file:`register.html` template for the function we used above, which106 takes advantage of the :file:`_formhelpers.html` template:107 108 .. sourcecode:: html+jinja109 17 unmodified lines