Reputation: 1999
I would like to render a flask_wtf
form with bootstrap/wtf.html
.
The form contains a regular SelectField
and a FieldList
of SelectFields.
To render the single SelectField works fine using the function wtf.form_field
.
However, applying the same function to each SelectField of the FieldList raises an error:
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
My interpretation of the error is that the string "field.label" was called like a function using parentheses. On the other hand, the same seems to work for the single SelectField.
Here is 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))
And here is 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 %}
Upvotes: 2
Views: 711
Reputation: 1999
I found the error source.
In my script, I assigned the labels of the selectfields of FormEntry
dynamically by
selectfield.label = "some_string"
However, the label of an SelectField
is no a string but an object that contains a string variable text
. Replacing the above line of code to
selectfield.label.text = "some_string"
did the job.
Upvotes: 1