I want to use WTForms to render a form in a table. It seems like the TableWidget will do the trick, but the only way I can get this to work is as follows:
from wtforms import Form, TextField, widgets
class User(Form):
user = TextField('User')
email = TextField('Password')
widget = widgets.TableWidget(with_table_tag=False)
user = User()
print user.widget(user)
This seems weird (the print user.widget(user)
part) According to the documentation, I ought to be able to say:
class User(Form):
user = TextField('User', widget=widgets.TableWidget)
email = TextField('Password', widget=widgets.TableWidget)
user = User()
for form_field in user:
print form_field
However, this returns TypeError: __str__ returned non-string (type TableWidget)
When I replace user, email with:
user = TextField('User')
email = TextField('Password')
Then of course the WTForms rendering works as expected.
How does this work?
In the docs it says the following about TableWidget
Renders a list of fields as a set of table rows with th/td pairs.
You are associating it a with single field instead of a list of fields. If you look in the code the __call__
method of TableWidget
expects an argument called field
but it treats it as an iterable
for purposes of generating the html string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With