我通过 Flask-WTF 生成一个表单。
相关复选框如下所示:
conditions = BooleanField(
"I agree to the above conditions.", validators=[DataRequired()])
我希望将我同意
加粗或加粗 - 句子的其余部分不。
我无法传入 HTML 标记,因为它们会被转义并呈现为文本。
这看起来像这样:
[] <strong>I agree</strong> to the above conditions.
我想得到这个结果:
我同意上述条件。
这可能吗?
感谢您的任何提示。
最佳答案
感谢 @gaefan 的其他回答,我阅读了有关 Jinja 模板和 safe
过滤器的更多信息,并提出了另一个可行的解决方案。
from flask import Markup
label_for_conditions = Markup('<span class="customClass">I agree</span> to the above conditions.')
conditions = BooleanField(label_for_conditions, validators=[DataRequired()])
此解决方案甚至不需要模板中的安全
过滤器。
这个答案受到以下讨论的启发:
Passing HTML to template using Flask/Jinja2
这不是一个完美的解决方案,因为现在 HTML 和 Python 混合在表单定义中,但看起来你必须做出妥协。
关于python - 是否可以在 Flask-WTForms 中设置标签的一部分样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63287032/