我想基于的字典定义带有字段的表单类name: label。我尝试了以下方法,这些方法几乎奏效了。但是,在模板中渲染字段给了AttributeError: 'UnboundField' object has no attribute '__call__'。如何动态向表单添加字段?
def build_form(name, record):
class ContactForm(FlaskForm):
name = StringField(name)
fieldlist = {}
for key, value in record.items():
fieldlist[key] = StringField(key)
@app.route('/', methods=['GET', 'POST'])
def showform():
form = ContactForm(request.form)
if request.method == 'POST':
return 'form processed'
return render_template('cardcompare.tpl', record=record, form=form)
Run Code Online (Sandbox Code Playgroud)
def build_form(name, record):
class ContactForm(FlaskForm):
name = StringField(name)
fieldlist = {}
for key, value in record.items():
fieldlist[key] = StringField(key)
@app.route('/', methods=['GET', 'POST'])
def showform():
form = ContactForm(request.form)
if request.method == …Run Code Online (Sandbox Code Playgroud)