【问题标题】:How to filter the html markups when render a template with jinja2?使用 jinja2 渲染模板时如何过滤 html 标记?
【发布时间】:2014-03-07 18:01:16
【问题描述】:

现在我正在使用 jinja2 处理模板来构建一个 django 项目。客户端使用所见即所得的编辑器提交了一些页面内容,并且在详细页面上一切正常。

但列表页面的内容切片有误。

我的代码:

<div class="summary ">
     <div class="content">{{ question.content[:200]|e}}...</div> 
</div>

但是输出是:

<p>what i want to show here &nbsp;is raw text without markups</p>...

预期的结果是像 &lt;p&gt;&lt;/p&gt; &lt;section&gt;.... 这样的 html 标记消失(过滤或消除)并且只显示原始文本!

那么我该如何解决呢?提前致谢!

【问题讨论】:

    标签: django templates jinja2


    【解决方案1】:

    使用striptags 过滤器:

    striptags(值)

    去除 SGML/XML 标记并替换相邻的空格 一个空格。

    <div class="content">{{ question.content|striptags}}...</div> 
    

    Jinja2 striptags filter test 也将帮助您了解它的工作原理。

    希望对您有所帮助。

    【讨论】:

    • 非常感谢{{ question.content|striptags|truncate(250, True)}}
    猜你喜欢
    • 1970-01-01
    • 2012-02-03
    • 2014-12-23
    • 2017-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-10
    • 2016-11-13
    相关资源
    最近更新 更多