How to decode &#39 in flask with Jinja2 template [duplicate]

ε祈祈猫儿з 提交于 2019-12-21 14:21:24

问题


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 %}

Errors are equal to

{'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.']}

回答1:


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

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

usage Example:

{{ errors|safe }}

Or to,

{{ errors | tojson | safe }}

Or can also mark it safe using Markup in Flask.



来源:https://stackoverflow.com/questions/39077534/how-to-decode-39-in-flask-with-jinja2-template

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