如何在django中完全按原样呈现代码块

时间:2017-03-07 00:25:35

标签: django django-templates

我如何正确呈现以下内容?

<pre>
    <code class="language-markdown">
        {% autoescape off %}
        <Item>hello</Item>
        {% endautoescape %}
    </code>
</pre>

基本上,我希望它显示xml,如:

<Item>Hello</Item>

不要试图剥离标签。相反,它只打印没有标签的“Hello”。请注意,verbatim标记对我来说也不起作用。

1 个答案:

答案 0 :(得分:0)

使用django.utils.html.escape https://docs.djangoproject.com/en/1.10/ref/utils/#django.utils.html.escape

在视图中执行此操作

或者编写一个模板标签,使用此功能进行转义。

from django import template
from django.utils.html import escape

register = template.Library()

@register.filter
def escape_html(value):
    return escape(value)