【问题标题】:How to put html in django template variables?如何将 html 放入 django 模板变量中?
【发布时间】:2011-09-26 10:10:00
【问题描述】:

我想在模板变量中放入 html 片段。像这样的:

>>>t = django.template.Template("<ul>{{ title }}<\ul>: {{ story }}")
>>>c = django.template.Context({"title":"This is the title",r"Line 1.<br />Line 2."})
>>>print t.render(c)
<ul>This is the title<\ul>: Line 1.&lt;br /&gt;Line 2.

我希望输出是这样的:

<ul>This is the title<\ul>: Line 1.<br />Line 2.

如何在模板变量中放入 HTML?

【问题讨论】:

    标签: python html django django-templates escaping


    【解决方案1】:

    使用safe 过滤器,例如:

    t = django.template.Template("{{ title|safe }}: {{ story|safe }}")

    【讨论】:

      【解决方案2】:

      使用safe 过滤器来禁止编码。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-02-14
        • 2012-12-11
        • 1970-01-01
        • 2021-03-14
        • 1970-01-01
        • 2013-11-27
        • 2023-04-05
        • 2016-04-22
        相关资源
        最近更新 更多