Ali*_*ani 3 django django-templates
我使用过,truncatechars但是如果文本包含html标签,它将为我显示该标签。
{{content.text|safe|truncatechars:140}}
Run Code Online (Sandbox Code Playgroud)
例如显示如下内容:
<p>hi</p>
Run Code Online (Sandbox Code Playgroud)
我要删除p标签。
与问题一起更新:
要在截断之前完全删除标签,请使用:
{{ content.text|striptags|truncatechars:140 }}
Run Code Online (Sandbox Code Playgroud)
原始答案:
为了确保正确地破坏标签,您将需要使用truncatechars_html模板过滤器。