Flask wtforms - 'UnboundField' object is not callable, dynamic field won't init properly

时光怂恿深爱的人放手 提交于 2019-12-24 03:08:24

问题


app.py

from flask import Flask, render_template
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField, FieldList, FormField

app = Flask(__name__)
app.config['SECRET_KEY'] = 'apple pie'


class BookForm(FlaskForm):
    book = StringField('book title')


class LibraryForm(FlaskForm):
    def __init__(self, min_entries=0, *args, **kwargs):
        super(LibraryForm, self).__init__(*args, **kwargs)
        self.books = FieldList(FormField(BookForm), min_entries=min_entries)

    library = StringField('Library name')
    books = FieldList(FormField(BookForm), min_entries=3)
    submit = SubmitField('Submit')


@app.route('/book', methods=['GET', 'POST'])
def book():
    form = LibraryForm(min_entries=5)
    if form.validate_on_submit():
        return 'aww yeah'
    return render_template('books.html', form=form)

books.html

<html>
<form method="POST" action="">
    {{ form.hidden_tag() }}
    <div>{{ form.library.label }}: {{ form.library() }}</div>
    <div>{{ form.books.label }}: {{ form.books() }}</div>
    <div>{{ form.submit.label }}: {{ form.submit() }}</div>
</html>

My goal is to make my form init with a flexible number of entries, such as in the example shown in the link. Whenever I run my code I get the following error:

TypeError: 'UnboundField' object is not callable

When I comment out the init function, the form works as expected, apart from the non-flexible amount of Book Fields. I've spent quite some time looking for answers, but couldn't find any which solved the problem.

example of how it should look

Any help is appeciated!


回答1:


It's not the ideal solution, but this works though.

from flask import Flask, render_template
from flask_wtf import FlaskForm, Form
from wtforms import StringField, SubmitField, FieldList, FormField

app = Flask(__name__)
app.config['SECRET_KEY'] = 'apple pie'


class BookForm(FlaskForm):
    book = StringField('book title')


class LibraryForm(FlaskForm):
    library = StringField('Library name')
    books = FieldList(FormField(BookForm))
    submit = SubmitField('Submit')


@app.route('/book', methods=['GET', 'POST'])
def book():
    form = LibraryForm()
    if form.validate_on_submit():
        return 'aww yeah'
    for i in range(6):
        form.books.append_entry()

    return render_template('books.html', form = form)


来源:https://stackoverflow.com/questions/48697016/flask-wtforms-unboundfield-object-is-not-callable-dynamic-field-wont-init

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!