im试图从Flask FormFields中的子表单获取输入数据。它可以工作,但是如果我更改表单中的值并提交,则只会得到旧值,而不会得到更改后的值。
这是我的应用代码:
from flask import Flask, render_template
from flask_wtf import FlaskForm
from wtforms import Form, FieldList, FormField, FloatField, SubmitField
class Subform(Form):
fee = FloatField()
class Mainform(FlaskForm):
fees = FieldList(FormField(Subform))
submit = SubmitField('Update!')
# Initialize app
app = Flask(__name__)
app.config["DEBUG"] = True
app.config['SECRET_KEY'] = 'sosecret'
@app.route('/', methods=['GET', 'Post'])
def init():
form = Mainform()
subform = Subform()
while len(form.fees) > 0:
form.fees.pop_entry()
for i in range(3):
subform.fee = i #inilialize the subforms with some data
form.fees.append_entry(subform)
if form.validate_on_submit():
for subform in form.fees:
print(subform.fee.data) #!!!
return render_template('webpage.html', form=form)
if __name__ == '__main__':
app.run()
这是我的html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form method="POST" action="">
{{ form.hidden_tag() }}
<p>forms to update:</p>
{% for subform in form.fees %}
{{subform.id}} {{subform.fee}}<br>
{% endfor %}
{{form.submit}}
</form>
</body>
</html>