How to use TableWidget WTForms?

I want to use WTForms to display a form in a table. It looks like TableWidget will do the trick, but the only way to get this to work:

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 (part of print user.widget(user) ). According to the documentation, I should 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 a TypeError: __str__ returned non-string (type TableWidget)

When I replace the user, write:

 user = TextField('User') email = TextField('Password') 

Then, of course, the WTForms rendering works as expected.

How it works?

+4
source share
1 answer

The docs say the following about TableWidget

Displays a list of fields as a set of table rows with th / td pairs.

You bind it to one field instead of a list of fields. If you look in the code, the __call__ TableWidget method expects an argument named field , but it treats it as iterable to generate an html string.

0
source

All Articles