gpt4 book ai didi

Flask wtforms - 'UnboundField' 对象不可调用,动态字段无法正确初始化

转载 作者:行者123 更新时间:2023-12-03 16:29:23 32 4
gpt4 key购买 nike

应用程序

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)

书籍.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>

我的目标是使用灵活数量的条目使我的表单初始化,例如在链接中显示的示例中。每当我运行我的代码时,都会出现以下错误:
TypeError: 'UnboundField' object is not callable

当我注释掉 初始化 功能,该表单按预期工作,除了非灵活数量的 Book Fields。我花了很长时间寻找答案,但找不到任何解决问题的方法。

example of how it should look

任何帮助是appeciated !

最佳答案

这不是理想的解决方案,但这确实有效。

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)

关于Flask wtforms - 'UnboundField' 对象不可调用,动态字段无法正确初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48697016/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com