Django将HTML对象作为纯文本传递到模板中 [英] Django passing HTML objects into template as plain text

查看:211
本文介绍了Django将HTML对象作为纯文本传递到模板中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这感觉就像是一个非常愚蠢的问题,但是我对博客创作模块django-zinnia的行为感到困惑。

This feels like a really silly issue, but I'm confused by the behavior of django-zinnia, a blog creation module.

当我测试输入一个简单的文本帖子,它附加每个句子与HTML<

When I test enter a plain text post, it appends each sentence with html < p > tags the browser doesn't read as html.

示例,如果我输入到数据库(no html):

Example, if I enter this into the database (no html):

从页面上的db呈现的条目就像这样, p>标记是纯文本:

The entry from the db renders on page itself like this as if the < p > markup was plain text:

在Zinnia中,这些html标签是在_entry_detail_base.html

Within Zinnia, these html tags are being generated as part of the {{ object_content }} object in _entry_detail_base.html

<div class="entry-content">
     {{ object_content }}
</div>

我查看了Zinnia中的entry.py模型,无法识别这些标签来自或如何传播,浏览器不会解释它们是什么(html)。有没有可以应用的过滤器可以解决这个问题?谢谢

I've looked through the entry.py models within Zinnia and I'm having trouble identifying where these tags are coming from or how they're being passed in in a way the browser doesn't interpret them for what they are (html). Is there a filter I can apply that might solve this? thanks

推荐答案

这是Django模板的默认行为。使用 {{object_content | safe}} {%autoescape off%} {{object_content}} {%endautoescape%} (用于多个变量)以防止html实体被转义。

That's the default behavior for Django templates. Use {{ object_content|safe }} or {% autoescape off %} {{ object_content }} {% endautoescape %} (for multiple variables) to prevent html entities from being escaped.

请注意,使用安全过滤器不会自动表示如果您之后使用其他过滤器,则不会转义输出它。

Note that using the safe filter doesn't automatically mean the output is not escaped if you use another filter after it.

这篇关于Django将HTML对象作为纯文本传递到模板中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆