我正在从数据库行动态创建一系列表单字段,每个字段都有自己的表单。提交表单时,它将更新数据库中的一行。我遇到的问题是如何将提交的表单与相关行匹配?所有表单字段都具有相同的名称“共享”,但这应该没问题,因为每个字段都有自己的表单。
视图.py
def setting(nickname):
user = User.query.filter_by(nickname = nickname).first()
cars = user.cars.all()
form_list = []
for car in cars:
class F(Form):
pass
setattr(F, 'shared', TextField(default=car.shared, label = car.carname))
form = F(request.form, obj = car)
form_list.append(form)
if form.validate_on_submit():
flash(request.form)
flash(form.shared.data)
return render_template('settings.html',
user = user,
form_list = form_list
)
设置.html
{% for field in form_list %}
<form action="" method="post" name="share">
{{ field.shared.label }} - {{ field.shared }}
<input type="submit" value="share/make private"/>
</form>
{% endfor %}
谢谢