Skip to main content
 首页 » 编程设计

python之从数据库 html 内容渲染一个 django 模板

2025年05月04日24lvdongjie

我的数据库内容中保存了原始 html。我想要呈现该内容,以下是方法,我尝试 -

    from django.template import Context, Template 
    l = MyModel.objects.get(slug=current_slug) 
    tpl = Template(escape(l.content_text)) 
    return HttpResponse(tpl.render(Context({}))) 

这将 html 呈现为字符串,在模板中我看到了 <html>.....</html>而不是呈现的内容。

让我知道如何解决这个问题。谢谢。

请您参考如下方法:

您需要 mark_safe() 而不是 escape()。