There are two solutions:
- When creating the Field using render_kw.
- When rendering the Field in the template (when the field is being called [
form.field()
]).
Initialize Field with style:
All the Field objects in WTForms have a render_kw arg in __init__
.
render_kw (dict) – If provided, a dictionary which provides default keywords that will be given to the widget at render time.
When your template is being rendered, Jinja2 is going to read this render_kw.
In your case, you can define:
conditions = BooleanField(
"I agree to the above conditions.", validators=[DataRequired()],
render_kw={"style": "font-weight: bold;")
Rendering while templating
When Jinja2 is rendering, you can specify other rendering options by calling the field.
form.field()
__call__(**kwargs)
: Render this field as HTML, using keyword args as additional attributes.
[...]
In all of the WTForms HTML widgets, keyword arguments are turned to
HTML attributes, though in theory a widget is free to do anything it
wants with the supplied keyword arguments, and widgets don’t have to
even do anything related to HTML.
So in your template file, you can do something like that:
{{ form.field(style="font-weight: bold;") }}
Note that there is an exception for the class keyword, probably reserved by something else, then the key should be class_
.
Source: Fields - WTForms Documencation