Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用引导呈现选定字段的WTF字段列表_Python_Flask_Flask Wtforms_Flask Bootstrap - Fatal编程技术网

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"
完成了任务