如何使用Jinja2模板解码烧瓶中的&#39

如何使用Jinja2模板解码烧瓶中的&#39

问题描述:

当我尝试从Jinja2模板中的wtforms写入错误时,它将返回未解码的报价.我该如何解决?

When I'm trying to write errors from wtforms in Jinja2 template, it returns undecoded quote. How can i fix it?

{% if registrationForm.errors %}
    <script>swal("Error!", "{{ registrationForm.errors['password'] }}", "error")</script>
{% endif %}

错误等于

{'email': ['This field is required.'], 'username': ['This field is required.'], 'acceptTOS': ['This field is required.'], 'csrf_token': ['CSRF token missing'], 'password': ['This field is required.']}

使用> c0> 模板过滤器-告诉jinja2不再应用其他过滤器.

Use the safe template filter - it tells jinja2 to not apply any further filters.

将字符串标记为可以安全地包含在HTML/XML输出中,而无需 需要逃脱.

Marks a string as being safe for inclusion in HTML/XML output without needing to be escaped.

用法示例:

{{ errors|safe }}

或者,

{{ errors | tojson | safe }}


或者也可以使用 Markup 将其标记为安全烧瓶.


Or can also mark it safe using Markup in Flask.