Django模板中的变量以双引号显示

时间:2018-04-22 06:44:08

标签: python django django-templates

我从Post模型中获取数据并简单地传递到模板中。但输出显示为双引号。 我在Post模型中有html标签,它不是渲染,而是显示为代码。

  

views.py

# It display the single post
def single_post(request,cat,post_id,post_title):
    single_post = Post.objects.get(pk = post_id)
    return render(request, 'blog/single_post.html', {'single_post': single_post})
  

single_post.html

<h1>I'm single post page</h1>

{{single_post.title}}

<br>
<br>
{{single_post.content}}
  

浏览器输出

enter image description here enter image description here

  

我的数据库中的数据   enter image description here

1 个答案:

答案 0 :(得分:2)

在屏幕截图中,我看不到双引号,我看到HTML标记。如果您要在内容中输入HTML,而不是让不受信任的用户输入HTML,那么您应该认为在页面上呈现是安全的(您不太可能发布恶意javascript,或者使用错误的HTML搞砸您的布局) , 对?)。因此,您希望在内容中使用Django的safe过滤器:

{{single_post.content|safe}}

这将允许解释输入和存储的HTML,而不是渲染。

相关问题