Django-将未转义的html存储在模型中 [英] Django - Store unescaped html in model
问题描述
我试图将原始的,未经转义的HTML存储在我的Django模型之一中,以显示在主页上。但是,当我将其存储在TextField中时,它会转义,并最终仅显示为原始文本。如何在Django模型中存储原始HTML?
I am trying to store raw, unescaped HTML inside one of my Django models for display on my home page. However, when I store it in a TextField it gets escaped, and ends up just being displayed as raw text. How can I store raw HTML in a Django model?
**编辑**
它不是在模型层而是在模板层逃脱。我应该使用特殊标签吗?我检查了外壳程序中的值,它很好,但是由于某种原因,当我在模板中进行{{block.html}(html是存储实际HTML的块对象的属性)时,结果如下:
It seems as if its not getting escaped in the model layer, but in the Template layer. Is there a special tag I should use? I checked the value in the shell and it's just fine, but for some reason when i did {{ block.html } (html is the attribute of the block object that stores the actual HTML) in the template, it comes out like this:
<p>This is a <strong>very</strong> <em>important</em> <span style="text-decoration: underline;">block</span></p>
<p style="padding-left: 30px;">it has very significant content!</p>
推荐答案
您可以使用安全
过滤器显示未转义的文本,或转义
过滤器显示未转义的文本。您还可以使用 autoescape
标记设置一个块。 ( {%时自动转义%}
或 {%时自动转义%}
)
You can use the safe
filter to present unescaped text, or escape
filter to present escaped text. You can also use autoescape
tag to set a block. ({% autoescape on %}
or {% autoescape off %}
)
这篇关于Django-将未转义的html存储在模型中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!