我想flask_wtf
用bootstrap/wtf.html
. 该表单包含一个常规SelectField
和一个FieldList
SelectFields。使用函数渲染单个 SelectField 可以正常工作wtf.form_field
。但是,对 FieldList 的每个 SelectField 应用相同的函数会引发错误:
File "/usr/local/lib/python3.5/dist-packages/flask_bootstrap/templates/bootstrap/wtf.html", line 119, in template
{{field.label(class="control-label")|safe}}
TypeError: 'str' object is not callable
我对错误的解释是字符串“field.label”被称为使用括号的函数。另一方面,这似乎也适用于单个 SelectField。
这是form.py:
from flask_wtf import FlaskForm
from wtforms import SelectField, FieldList, FormField
class FormEntry(FlaskForm):
selectfield = SelectField('Name', coerce=int)
class MyForm(FlaskForm):
selectfield = SelectField('Name', coerce=int, choices=[(2, "choice 2"), (1, "choice 1")])
form_entries = FieldList(FormField(FormEntry))
这是render.html:
{% extends 'bootstrap/base.html' %}
{% import 'bootstrap/wtf.html' as wtf %}
{{ form.hidden_tag() }}
{{ wtf.form_field(form.selectfield) }}
{% for entry in form.form_entries %}
{{ wtf.form_field(entry.selectfield) }}
{% endfor %}