The PyCK Forms Package
The Form base class, extends from WTForms.Form
This class provides some additional features to the base wtforms.Form class. These include:
CSRF protection availability
TODO: Since WTForms wants presentation attributes (like rows and cols for textarea) to be set on
instantiated form instances (not the class itself), allow some way to specify field attributes that are then read at field display time. Possibly a field_attrs dict??? May be reimplementing sub-classes of some fields to make use of it would be a good idea??
Output each form field as html p tags. By default labels are displayed on top of the form fields and validation erros are displayed on the right of the form fields. Both these behaviors can be changed by settings values for the labels and errors parameters.
Values can be left, top, right or bottom
| Parameters: |
|
|---|
Output each form field as html p tags. By default labels are displayed on top of the form fields and validation erros are displayed on the right of the form fields. Both these behaviors can be changed by settings values for the labels and errors parameters.
Values can be left, top, right or bottom
| Parameters: |
|
|---|
Output the form as HTML Table, optionally add the table tags too if include_table_tag is set to True (default False)
| Parameters: |
|
|---|
Validate form fields and check for CSRF token match if use_csrf_protection was set to true when initializing the form.
A Wrapper around wtforms.ext.sqlalchemy.orm.model_form() function to facilitate creating model forms using a wtforms compatible model_form call but using pyck.forms.Form Create a wtforms Form for a given SQLAlchemy model class:
from pyck.forms import model_form
from myapp.models import User
UserForm = model_form(User)
| Parameters: |
|
|---|
A Wrapper around wtforms.ext.sqlalchemy.orm.model_form() function to facilitate creating model forms using a wtforms compatible model_form call but using pyck.forms.Form and :module:`WTDojo` form components
Create a wtforms Form for a given SQLAlchemy model class:
from pyck.forms import dojo_model_form
from myapp.models import User
UserForm = dojo_model_form(User)
| Parameters: |
|
|---|
Here is an example usage to demonstrate the improvements:
from pyck.forms import Form
from wtforms import TextField, validators
class MyForm(Form):
name = TextField("Name", [validators.required()])
fname = TextField("Father's Name", [validators.required()])
myform = MyForm()
str(myform.as_p())
r'\n<p>\n<label for="name">Name</label><br /> <input id="name" name="name" type="text" value="" /> </p>\n\n<p>\n<label for="fname">Father\'s Name</label><br /> <input id="fname" name="fname" type="text" value="" /> </p>\n'
myform.validate()
False
str(myform.as_p(labels='left', errors='right'))
'\n<p>\n<label for="name">Name</label> <input id="name" name="name" type="text" value="" /> <span class="errors">This field is required.</span> </p>\n\n<p>\n<label for="fname">Father\'s Name</label> <input id="fname" name="fname" type="text" value="" /> <span class="errors">This field is required.</span> </p>\n'
Within a template, all you need to do is to just call the form’s rendering function, for example:
${myform.as_p() | n}