问题
I'm writing a Jinja macro to render some form data in a template, but for some reason the form data renders as escaped text instead of html! Here is my macro, with the first row of the form called:
{% macro formrow(field) %}
<tr>
<td>form.{{ field }}.label|safe</td>
<td>form.{{ field }}|safe</td>
<td>form.{{ field }}.help_text|safe</td>
<td>form.{{ field }}.errors|safe</td>
</tr>
{% endmacro %}
{{ formrow('item_name') }}
Ideas? What am I missing?
回答1:
It seems you are not getting the Jinja template syntax completely yet, regardless, I would recommend doing something like this:
{% macro formrow(field) %}
<tr>
<td>form.{{ field.label_tag() }}</td>
<td>form.{{ field.as_widget() }}</td>
<td>form.{{ field.help_text|safe }}</td>
<td>form.{{ field.errors.as_ul() }}</td>
</tr>
{% endmacro %}
{{ formrow('item_name') }}
来源:https://stackoverflow.com/questions/18811748/why-does-jinja-escape-html-in-a-macro