Problem with setting up dynamic list of subforms with flask, wtforms and jinja2 Problem with setting up dynamic list of subforms with flask, wtforms and jinja2 flask flask

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:

  1. 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 to append_entry(). It needs to be a dictionary object.)
  2. You should call append_entry() after the validate_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.
  3. 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.
  4. You mentioned CSRF. You need to include {{ entry_line.hidden_tag() }} inside the subform for loop. That should be all you need to get CSRF working with the subform.

Try that and see if your form starts working.