Embed HTML tag in Flask WTForms field

夙愿已清 提交于 2019-12-11 04:42:38

问题


I'm using Flask with Jinja2/WTForms to build a login page and I want to customize a "Remember Me" checkbox. To do this, I would like to embed an empty <span> tag into the form's label field. Here is the Jinja2 code in the HTML file:

{{ login_user_form.remember|safe }} {{ login_user_form.remember.label }}

which generates the following HTML:

<input id="remember" name="remember" type="checkbox" value="y"> <label for="remember">Remember Me</label>

However, the following HTML is what I would ultimately like it to be - notice the <span> tag embedded within the label.

<input id="remember" name="remember" type="checkbox" value="y"> <label for="remember"><span></span>Remember Me</label>

回答1:


Include the span tag in your label name and use the safe filter.

remember = YourField(name='<span></span>Remember Me')

And then

{{ login_user_form.remember|safe }} {{ login_user_form.remember.label|safe }}


来源:https://stackoverflow.com/questions/41795398/embed-html-tag-in-flask-wtforms-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!