Pretty forms with uni-form and WTForms
wtfuniform module extends many of the
wtforms classes with the needed functionality. Since it imports all the
wtforms names and has the same structure, you can use it the same way by changing the import.
# note that the only difference from normal wtforms usage is importing # from a different module! from wtfuniform import TextField, PasswordField, SubmitField, Form, validators class LoginForm(Form): username = TextField('Your username', [validators.Required()]) password = PasswordField('Your password', [validators.Required()]) login = SubmitField('Login') # optional, see below: render the form from wtfuniform.helper import render_form render_form(LoginForm())
The module in detail
To properly work with uni-form, many of the WTForms widgets are extended with extra functionality:
- Widgets have the proper CSS classes (such as
textInputfor text input fields).
- CSS classes for client-side validation are added, uni-form will validate accordingly.
BlockLabelsWidgetis a widget that functions almost like a
ListWidget, but omits the labels. It is used in conjunction with
CheckboxLabeledInputto produce the proper multi checkbox/radio inputs used in uni-form.
- All of the above is easily accessible by the
CheckMultipleFieldfor checkboxes. The default
RadioFieldis already overriden to use these new widgets.
- There is a helper module
wtfuniform.helperthat includes two functions for HTML code generation:
render_form. For an example on how to use these with jinja2, see formtest.html and simpletest.py.
There are some extensions to the code that are not mandated by uni-form. These included:
DateTimeFieldhave extra css classes of