【问题标题】:Why does Jinja escape html in a macro?为什么 Jinja 在宏中转义 html?
【发布时间】:2013-09-19 15:25:23
【问题描述】:

我正在编写一个 Jinja 宏来在模板中呈现一些表单数据,但由于某种原因,表单数据呈现为转义文本而不是 html!这是我的宏,表单的第一行称为:

{% 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') }}

想法?我错过了什么?

【问题讨论】:

  • 确切的输出是什么?

标签: html django macros escaping jinja2


【解决方案1】:

看来您还没有完全掌握 Jinja 模板语法,无论如何,我建议您这样做:

{% 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') }}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-18
    • 2012-08-12
    • 1970-01-01
    相关资源
    最近更新 更多