Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use WTForms' TableWidget?

Tags:

python

wtforms

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?

like image 946
poundifdef Avatar asked Apr 03 '13 15:04

poundifdef


1 Answers

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.

like image 66
nsfyn55 Avatar answered Oct 28 '22 07:10

nsfyn55