python-如何使用Jinja2模板解码烧瓶中的'
作者:互联网
这个问题已经在这里有了答案: > JavaScript raises SyntaxError with data rendered in Jinja template 2个
当我尝试在Jinja2模板中从wtforms写入错误时,它将返回未解码的报价.我该如何解决?
{% 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.']}
解决方法:
使用safe
模板过滤器-它告诉jinja2不要应用任何其他过滤器.
Marks a string as being safe for inclusion in HTML/XML output without
needing to be escaped.
用法示例:
{{ errors|safe }}
或者,
{{ errors | tojson | safe }}
或者也可以使用Flask中的Markup
将其标记为安全.
标签:flask-wtforms,jinja2,flask,python 来源: https://codeday.me/bug/20191118/2026859.html