簡體   English   中英

如何使用WTForms的TableWidget?

[英]How to use WTForms' TableWidget?

我想使用WTForms在表格中呈現表單。 似乎TableWidget可以解決這個問題,但是我能讓它工作的唯一方法如下:

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)

這看起來很奇怪( print user.widget(user)部分)根據文檔,我應該能夠說:

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

但是,這會返回TypeError: __str__ returned non-string (type TableWidget)

當我替換用戶時,發送電子郵件至:

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

當然,WTForms渲染按預期工作。

這是如何運作的?

在文檔中,它描述了關於TableWidget的以下內容

將字段列表呈現為具有th / td對的一組表行。

您將它與單個字段而不是字段列表相關聯。 如果查看代碼, TableWidget__call__方法需要一個名為field的參數,但為了生成html字符串,它會將其視為iterable的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM