Why does Jinja escape html in a macro?

ⅰ亾dé卋堺 提交于 2020-01-05 05:46:09

问题


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

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