2012-07-15 66 views
1

使用wtforms我想格式化下面的表單。所以我有evrything工作,但我不需要'FormField'被包裹在HTML表 - 所以如何繞過/重寫呢?在WTForms中覆蓋html格式的FormField

class WeekdayHoursForm(BaseForm): 

    hours = [(str(x+1), str(x+1)) for x in range(12)] 
    mins=[('00',':00'),('15',':15'),('30',':30'),('45',':45')] 

    fromHour = SelectField('',choices=hours) 
    fromMin = SelectField('',choices=mins) 
    toHour = SelectField('',choices=hours) 
    toMin = SelectField('',choices=mins) 
    closed = BooleanField('Closed','0') 

class AddListingForm(BaseForm): 
    monday = FormField(WeekdayHoursForm) 
    tuesday = FormField(WeekdayHoursForm) 

    etc... 

我查看

<div class="wrapper-block weekday" id="mon"> 
    {{ form.monday.label }} {{ form.monday() }} 
</div> 

<div class="wrapper-block weekday" id="tues"> 
    {{ form.tuesday.label }} {{ form.tuesday() }} 
</div> 

我省略了生成的HTML,但如果需要,我可以將其粘貼 - 我知道wtforms FormField採用了「TableWidget」,但我不知道在哪裏可以將它設置爲'with_table_tag = False'

回答

1

每個wtforms.fields.Field子類都需要一個名爲widget的關鍵字參數,它可以用來覆蓋該字段的呈現。您可以閱讀更多關於可用的WTForms小部件in their docs

如果你只是想用一個列表,而不是一個表,你可以使用內置的ListWidget:

# ... snip ... 
class AddListingForm(BaseForm): 
    monday = FormField(WeekdayHoursForm, widget=ListWidget) 
    tuesday = FormField(WeekdayHoursForm, widget=ListWidget) 
+0

謝謝 - 我諮詢了文檔,但仍然不知道在哪裏/如何在widget傳遞。 – rebelbass 2012-07-20 02:58:30