I'm creating a series of form fields dynamically from database rows, each field will have it's own form. When a form is submitted it will update a row in the database. The problem I have is how do I match the submitted form up with the relevant row? All the form fields have the same name 'shared' but this should be ok as each field has it's own form.
views.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
)
settings.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 %}
Thanks