Python 使用引导呈现选定字段的WTF字段列表
我想用Python 使用引导呈现选定字段的WTF字段列表,python,flask,flask-wtforms,flask-bootstrap,Python,Flask,Flask Wtforms,Flask Bootstrap,我想用bootstrap/wtf.html呈现一个flask\uwtf表单。 该表单包含一个常规的SelectField和一个选择字段的FieldList。 使用函数wtf.form\u field渲染单个SelectField效果良好。 但是,对FieldList的每个SelectField应用相同的函数会引发错误: File "/usr/local/lib/python3.5/dist-packages/flask_bootstrap/templates/bootstrap/wtf.ht
bootstrap/wtf.html
呈现一个flask\uwtf
表单。
该表单包含一个常规的SelectField
和一个选择字段的FieldList
。
使用函数wtf.form\u field
渲染单个SelectField效果良好。
但是,对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 %}
我找到了错误源。
在我的脚本中,我通过
selectfield.label = "some_string"
但是,SelectField
的标签不是字符串,而是包含字符串变量text
的对象。将上述代码行替换为
selectfield.label.text = "some_string"
完成了作业。我找到了错误源。
在我的脚本中,我通过
selectfield.label = "some_string"
但是,SelectField
的标签不是字符串,而是包含字符串变量text
的对象。将上述代码行替换为
selectfield.label.text = "some_string"
完成了任务