Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add an attribute that contains a hyphen to a WTForms field

Tags:

python

wtforms

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?

like image 821
Ryan Avatar asked Sep 05 '12 15:09

Ryan


1 Answers

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.

like image 110
Warren Weckesser Avatar answered Oct 15 '22 17:10

Warren Weckesser