如何打印视图返回的上下文添加的模板



只是想知道,我想将印刷的html以及我视图返回的上下文以及我的视图返回的上下文。(我只想将其打印在服务器端(。 例如

def my_view(request):
    template='product/compare-in-box.html'
    context={"data": 'check',}
    # print(render(request , template ,context))   the thing i was trying to print but not working as i expect.
    return render(request , template,context)

product/compare-in-box.html包含

<strong>{{data}}</strong>

我想要" prind(Render(请求,模板,context(("才能给我结果:
<stong> check </stong>

如何实现?

您可以使用 render_to_string 像此 render_to_string('compare-in-box.html', context)这样做。它将以字符串形式提供整个HTML模板,并在模板内放置上下文数据。参考

最新更新