【问题标题】:How to save Html code in database through Django如何通过 Django 将 Html 代码保存在数据库中
【发布时间】:2019-04-27 17:40:47
【问题描述】:

我想通过 django 将html code 保存到数据库中。

models.py 我有这个字段

description = models.TextField()

但是当我通过 Django Admin 将数据保存在 description field 中时。它显示这样的数据

<h1>This is example</h1>

然后将其转换为 html。 Here's the Out Put

【问题讨论】:

    标签: html django python-3.x


    【解决方案1】:

    模板引擎转义 html 默认值。显示时必须使用安全标签对其进行过滤。

    {{ description |safe }}
    

    或使用自动转义标签

    {% autoescape off %}{{ description }}{% endautoescape %}
    

    【讨论】:

    • 我可以在哪里使用这些标签?在 view.py 或 models.py 中
    • 或在我看来.html
    • 它应该在你的视野中。
    • 感谢它的工作..我只是把这段代码放在 view.html 中,就像这样 {% autoescape off %}{{object.description |换行符 }}{% endautoescape %}
    • @devdap 当你说它应该在你的视图中时,你需要小心。 Django 中的 View 等价于其他框架中的 Controller。所以我会说“它应该在你的模板中,即 template.html”
    猜你喜欢
    • 2013-08-27
    • 2017-07-25
    • 1970-01-01
    • 2015-05-04
    • 1970-01-01
    • 2016-07-29
    • 2021-02-22
    • 2021-10-04
    • 2021-09-03
    相关资源
    最近更新 更多