Calling a WTForms field object produces the rendered field, and any arguments are taken as attributes, for instance.
form.field(attribute='value')
would return something like
<input attribute='value'>
How can I add HTML5 custom data attributes such as data-provide which contain hyphens, making them unparseable in python as a single keyword argument?
Create a dictionary with the corresponding key-value pairs and use ** to pass it to the field call:
attrs = {'data-provide': "foo"}
form.field(**attrs)
Edit: Looks like the comment by @NiklasB should be part of the answer:
For those using flask with flask-WTF, use: {{ form.field( **{'data-provide': 'foo'} ) }}
in your template.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With