问题
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