Making some Python corrections to the WTForms examples.
This commit is contained in:
@@ -12,34 +12,37 @@ This article details best practices for integrating the popular forms library,
|
|||||||
Defining a Form Definition
|
Defining a Form Definition
|
||||||
--------------------------
|
--------------------------
|
||||||
|
|
||||||
A basic form with a first name field that is required and a last name optional
|
Let's start by building a basic form with a required ``first_name`` field and an optional ``last_name`` field::
|
||||||
field.
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
from wtforms import Form, TextField, validators
|
from wtforms import Form, TextField, validators
|
||||||
|
|
||||||
|
class MyForm(Form):
|
||||||
|
first_name = TextField(u'First Name', validators=[validators.required()])
|
||||||
|
last_name = TextField(u'Last Name', validators=[validators.optional()])
|
||||||
|
|
||||||
class SomeController(object):
|
class SomeController(object):
|
||||||
...
|
pass
|
||||||
class MyForm(Form):
|
|
||||||
first_name = TextField(u'First Name', validators=[validators.required()])
|
|
||||||
last_name = TextField(u'Last Name', validators=[validators.optional()])
|
|
||||||
|
|
||||||
Rendering a Form in a Template
|
Rendering a Form in a Template
|
||||||
------------------------------
|
------------------------------
|
||||||
|
|
||||||
Using that same MyForm definition we can render it to a template.
|
Next, let's add a controller, and pass a form instance to the template::
|
||||||
|
|
||||||
In the controller file:
|
from pecan import expose
|
||||||
|
from wtforms import Form, TextField, validators
|
||||||
|
|
||||||
.. code-block:: python
|
class MyForm(Form):
|
||||||
|
first_name = TextField(u'First Name', validators=[validators.required()])
|
||||||
|
last_name = TextField(u'Last Name', validators=[validators.optional()])
|
||||||
|
|
||||||
class SomeController(object):
|
class SomeController(object):
|
||||||
@expose(template='index.html)
|
|
||||||
def index(self):
|
|
||||||
return dict(form=self.MyForm())
|
|
||||||
|
|
||||||
In the template file using `Mako <http://www.makeotemplates.org/>`_:
|
@expose(template='index.html')
|
||||||
|
def index(self):
|
||||||
|
return dict(form=MyForm())
|
||||||
|
|
||||||
|
Here's the template file (which uses the `Mako <http://www.makeotemplates.org/>`_
|
||||||
|
templating language):
|
||||||
|
|
||||||
.. code-block:: html
|
.. code-block:: html
|
||||||
|
|
||||||
@@ -58,21 +61,25 @@ In the template file using `Mako <http://www.makeotemplates.org/>`_:
|
|||||||
Validating POST Values
|
Validating POST Values
|
||||||
----------------------
|
----------------------
|
||||||
|
|
||||||
Using the same MyForm definition we will redirect if the form is validated,
|
Using the same ``MyForm`` definition, let's redirect the user if the form is
|
||||||
otherwise, render the form again.
|
validated, otherwise, render the form again.
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
from pecan import request
|
from pecan import expose, request
|
||||||
|
from wtforms import Form, TextField, validators
|
||||||
|
|
||||||
|
class MyForm(Form):
|
||||||
|
first_name = TextField(u'First Name', validators=[validators.required()])
|
||||||
|
last_name = TextField(u'Last Name', validators=[validators.optional()])
|
||||||
|
|
||||||
class SomeController(object):
|
class SomeController(object):
|
||||||
@expose(method='POST', template='index.html')
|
|
||||||
|
@expose(template='index.html')
|
||||||
def index(self):
|
def index(self):
|
||||||
my_form = MyForm(request.POST)
|
my_form = MyForm(request.POST)
|
||||||
if request.method == 'POST' && my_form.validate():
|
if request.method == 'POST' and my_form.validate():
|
||||||
save_values()
|
# save_values()
|
||||||
redirect('/success')
|
redirect('/success')
|
||||||
else:
|
else:
|
||||||
return dict(form=my_form)
|
return dict(form=my_form)
|
||||||
...
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user