Setting data attributes in WTForms field

I want to add "data-" attributes to a form field for integration with Bootstrap. I tried the following in a template:

{{ form.test(data-toggle="toggle", data-size="mini", data-on="Yes", data-off="No", type="checkbox")}}

      

and got this error:

TemplateSyntaxError: expected token ',', got '='

      

Why did I get this error and how can I fix it?

+3


source to share


1 answer


You need to use valid Python names as variable names. Therefore, names like "data-toggle" are invalid because they contain "-". Change the names to use underscores, such as "data_toggle". WTForms will automatically convert "_" to "-" for keywords it doesn't recognize.



+4


source







All Articles