Problem with setting up dynamic list of subforms with flask, wtforms and jinja2
You have the right idea, but there are a few issues with what you've done:
- You shouldn't explicitly create the SubForm instances. Pass a dictionary to
append_entry()
to populate the fields in the subform. (Edit: Took out incorrect information about passing form instances toappend_entry()
. It needs to be a dictionary object.) - You should call
append_entry()
after thevalidate_on_submit()
block, not before. When the POST request comes back with your form, it will already have sufficient subforms created. You did that when the page was built. You just need to read all the form contents and pull out/save your data before the redirect. - You mentioned missing data and uncalled validation. I have a hunch that at present you're overwriting the form data before the validation method is called. So this problem may resolve itself.
- You mentioned CSRF. You need to include
{{ entry_line.hidden_tag() }}
inside the subformfor
loop. That should be all you need to get CSRF working with the subform.
Try that and see if your form starts working.