2

我想flask_wtfbootstrap/wtf.html. 该表单包含一个常规SelectField和一个FieldListSelectFields。使用函数渲染单个 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 %}
4

1 回答 1

1

我找到了错误来源。在我的脚本中,我FormEntry动态分配了选择字段的标签

selectfield.label = "some_string"

但是,an 的标签SelectField不是字符串,而是包含字符串变量的对象text。将上面的代码行替换为

selectfield.label.text = "some_string"

做了这项工作。

于 2019-06-10T14:57:30.327 回答