python - Django 模板中的变量显示在双引号中

标签 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}}

Browser Output

enter image description here enter image description here

Data in my Database enter image description here

最佳答案

在你的截图中,我没有看到双引号,我看到了 HTML 标签。如果你在内容中输入 HTML,而不是让不受信任的用户输入它,那么你应该考虑将其呈现到页面上是安全的(你不太可能发布恶意 javascript,或者用糟糕的 HTML 搞砸你的布局,对吧?)。因此,您想对您的内容使用 Django 的 safe 过滤器:

{{single_post.content|safe}}

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

关于python - Django 模板中的变量显示在双引号中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49963291/

相关文章:

python - 如何诊断消失的端口监听器?

python - Django 相当于 Rails 的 x.days.from_now

django - 从Docker包含Django App访问Windows定位的Firebird数据库

error-handling - 在以下情况下如何在smarty模板中使用变量?

python - 无法在模板中显示外键中的相关字段

python - 引发 Http404() 时未调用 Django handler404

python - 在python中解析json

python - 读取二进制文件并遍历每个字节

没有子查询的Python mysql更新导致子查询错误

python - 如何添加到导航pinax(0.9a2)的链接?