Render WTForms SelectField Options with Inline HTML in Jinja

I am trying to create a parameter in SelectField

that uses inline span

in a label. However, the tag is escaped and appears literally in the field.

I've tried Insert HTML Tag into Flask WTForms field , but it doesn't work in this case (probably because the text to be escaped is inside the list).

Is it possible to render HTML without a label in a parameter label without recording my own rendering?

class myForm(Form):
    myChoices = [
        ('0','Select an option <span class="caret"></span>'),
        ('1','Option 1'),
        ('2','Option 2')
    ]
    optionSelect = SelectField('Select', choices=myChoices, validators=[Required()])

      

+3


source to share


1 answer


Like your related question, you should let Jinja know that the value you are showing is reliable and should not be escaped. Since you do not directly control the rendering of the parameters, you cannot use a filter |safe

, and you will need to do so when defining the value. Use a class Markup

to mark a string as safe in Python.



from jinja2 import Markup

('0', Markup('Select an option <span class="caret"></span>')),

      

0


source







All Articles