Python 是否可以将标签的一部分样式化?

Python 是否可以将标签的一部分样式化?,python,html,flask,flask-wtforms,Python,Html,Flask,Flask Wtforms,我通过Flask WTF生成一个表单 相关复选框如下所示: conditions = BooleanField( "I agree to the above conditions.", validators=[DataRequired()]) [] <strong>I agree</strong> to the above conditions. 我想让我同意加粗或加粗-不要句子的其余部分 我无法传入HTML标记,因为它们被

我通过Flask WTF生成一个表单

相关复选框如下所示:

    conditions = BooleanField(
        "I agree to the above conditions.", validators=[DataRequired()])
[] <strong>I agree</strong> to the above conditions.
我想让
我同意
加粗或加粗-不要句子的其余部分

我无法传入HTML标记,因为它们被转义并呈现为文本

这看起来像这样:

    conditions = BooleanField(
        "I agree to the above conditions.", validators=[DataRequired()])
[] <strong>I agree</strong> to the above conditions.
[]我同意上述条件。
我想得到这个结果:

我同意上述条件

这可能吗


谢谢你的提示。

一个简单的解决方案就是在模板中完成。而不是:

{{ user_form.conditions.errors }}{{ user_form.conditions.label_tag }}<br />{{ user_form.conditions }}
{{user\u form.conditions.errors}{{user\u form.conditions.label\u tag}}
{{user\u form.conditions}
做:

{{user\u form.conditions.errors}我同意上述条件。
{{{user\u form.conditions}
我尚未对此进行测试,但您可能可以:

conditions = BooleanField(
    "<strong>I agree</strong> to the above conditions.", validators=[DataRequired()])
conditions=BooleanField(
“我同意上述条件。”,验证程序=[DataRequired()])
然后,在模板中:

{{ user_form.conditions.errors }}{{ user_form.conditions.label_tag|safe }}<br />{{ user_form.conditions }}
{{user_form.conditions.errors}{{user_form.conditions.label_tag|safe}}
{{user_form.conditions}
一个简单的解决方案就是在模板中完成。而不是:

{{ user_form.conditions.errors }}{{ user_form.conditions.label_tag }}<br />{{ user_form.conditions }}
{{user\u form.conditions.errors}{{user\u form.conditions.label\u tag}}
{{user\u form.conditions}
做:

{{user\u form.conditions.errors}我同意上述条件。
{{{user\u form.conditions}
我尚未对此进行测试,但您可能可以:

conditions = BooleanField(
    "<strong>I agree</strong> to the above conditions.", validators=[DataRequired()])
conditions=BooleanField(
“我同意上述条件。”,验证程序=[DataRequired()])
然后,在模板中:

{{ user_form.conditions.errors }}{{ user_form.conditions.label_tag|safe }}<br />{{ user_form.conditions }}
{{user_form.conditions.errors}{{user_form.conditions.label_tag|safe}}
{{user_form.conditions}
有两种解决方案:

  • 使用渲染创建字段时
  • 在模板中呈现字段时(调用字段[
    form.Field()
    ])

使用样式初始化字段: WTForms中的所有表单在
\uuuuu init\uuuu
中都有一个渲染参数

render_kw(dict)–如果提供,则是一个字典,提供在呈现时提供给小部件的默认关键字

在渲染模板时,Jinja2将读取此render\u kw。 在您的情况下,您可以定义:

conditions = BooleanField(
    "I agree to the above conditions.", validators=[DataRequired()],
    render_kw={"style": "font-weight: bold;")
模板化时的渲染 当Jinja2渲染时,可以通过调用字段指定其他渲染选项

form.field()
\uuuu调用(**kwargs)
:使用关键字args作为附加属性,将此字段呈现为HTML

[……]

在所有的WTForms HTML小部件中,关键字参数都是 HTML属性,尽管在理论上小部件可以自由地执行任何操作 需要使用提供的关键字参数,而小部件不必这样做 甚至做任何与HTML相关的事情

因此,在模板文件中,可以执行以下操作:

{{ form.field(style="font-weight: bold;") }}
请注意,class关键字有一个例外,可能是由其他内容保留的,那么该关键字应该是
class\uuu



有两种解决方案:

  • 使用渲染创建字段时
  • 在模板中呈现字段时(调用字段[
    form.Field()
    ])

使用样式初始化字段: WTForms中的所有表单在
\uuuuu init\uuuu
中都有一个渲染参数

render_kw(dict)–如果提供,则是一个字典,提供在呈现时提供给小部件的默认关键字

在渲染模板时,Jinja2将读取此render\u kw。 在您的情况下,您可以定义:

conditions = BooleanField(
    "I agree to the above conditions.", validators=[DataRequired()],
    render_kw={"style": "font-weight: bold;")
模板化时的渲染 当Jinja2渲染时,可以通过调用字段指定其他渲染选项

form.field()
\uuuu调用(**kwargs)
:使用关键字args作为附加属性,将此字段呈现为HTML

[……]

在所有的WTForms HTML小部件中,关键字参数都是 HTML属性,尽管在理论上小部件可以自由地执行任何操作 需要使用提供的关键字参数,而小部件不必这样做 甚至做任何与HTML相关的事情

因此,在模板文件中,可以执行以下操作:

{{ form.field(style="font-weight: bold;") }}
请注意,class关键字有一个例外,可能是由其他内容保留的,那么该关键字应该是
class\uuu



多亏了@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()])
从flask导入标记
label_for_conditions=标记('我同意上述条件')
条件=BooleanField(为条件添加标签,验证器=[DataRequired()]))
此解决方案甚至不需要模板中的
safe
过滤器

以下讨论启发了这个答案:


这不是一个完美的解决方案,因为现在HTML和Python混合在表单定义中,但看起来您必须做出妥协。

多亏了@gaefan的另一个答案,我阅读了更多关于Jinja模板和
安全过滤器的内容,并提出了另一个可行的解决方案

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()])
从flask导入标记
label_for_conditions=标记('我同意上述条件')
条件=BooleanField(为条件添加标签,验证器=[DataRequired()]))
此解决方案甚至不需要模板中的
safe
过滤器

以下讨论启发了这个答案:


这不是一个完美的解决方案,因为现在HTML和Python混合在表单定义中,但看起来您必须做出折衷。

我在Flask 1.1.2上使用了WTForm 2.3.3,最后我使用了以下内容,因为标签本身是管道