Skip to content

Instantly share code, notes, and snippets.

@tassaron
Created November 2, 2020 14:02
Show Gist options
  • Save tassaron/b17d16b502c13431cc5dfe90aa51dadc to your computer and use it in GitHub Desktop.
Save tassaron/b17d16b502c13431cc5dfe90aa51dadc to your computer and use it in GitHub Desktop.
get all data from fields in a WTForm as dict
"""
It took me hours to figure out that iterating over fields
in a WTForm has unintuitive behaviour if you use `continue`
"""
# This works as expected
for field in form._fields:
data[field] = form._fields[field].data
del data["csrf_token"]
del data["submit"]
# This doesn't work as expected
for field in form._fields:
if field in ("csrf_token", "submit"):
continue
data[field] = form._fields[field].data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment