我想用渲染一个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 %}
我发现了错误源。在我的脚本中,我通过以下方式FormEntry
动态分配了选择字段的标签:
selectfield.label = "some_string"
但是,an的标签SelectField
不是字符串,而是包含string变量的对象text
。将上面的代码行替换为
selectfield.label.text = "some_string"
做好了
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句