如何在 Django 模板中将 python 转换为 html
how to convert python to html in django template
我在 views.py[=15= 中使用 json2html 将 json 值转换为 html tables ]
render(request, 'home.html', {'value': json2html.convert(json=result)})
其中结果有 json 个数据。
上面为 json 提供了 html 但是在 home.html 模板中它没有以 html table 格式显示而是显示如下在网站上
<ul><li><table border="1"><tr><th>name</th><td>Test</td></tr><tr><th>
我的 home.html 代码看起来像这样
<html>
<body>
{{ value }}
</body>
</html>
变量值具有转换后的 html table 代码,但它显示为原始代码,而不是应呈现为 html 代码。如何做到这一点?
HTML
<script>
$(document).ready(function(){
$.ajax({
type: "GET",
url: "your home view url",
success: function (response) {
$('body').html(response)
}
});
})
</script>
views.py
return Response('<ul><li><table border="1"><tr><th>name</th><td>Test</td></tr><tr><th>')
如有帮助请点个赞
告诉Django一个字符串是安全的,不会自动转义:
{{ value|safe }}
我在 django.utils 下找到了一个叫做 format_html 的东西。
所以代码看起来像
render(request, 'home.html', {'value': format_html(json2html.convert(json=res))})
谢谢
我在 views.py[=15= 中使用 json2html 将 json 值转换为 html tables ]
render(request, 'home.html', {'value': json2html.convert(json=result)})
其中结果有 json 个数据。
上面为 json 提供了 html 但是在 home.html 模板中它没有以 html table 格式显示而是显示如下在网站上
<ul><li><table border="1"><tr><th>name</th><td>Test</td></tr><tr><th>
我的 home.html 代码看起来像这样
<html>
<body>
{{ value }}
</body>
</html>
变量值具有转换后的 html table 代码,但它显示为原始代码,而不是应呈现为 html 代码。如何做到这一点?
HTML
<script>
$(document).ready(function(){
$.ajax({
type: "GET",
url: "your home view url",
success: function (response) {
$('body').html(response)
}
});
})
</script>
views.py
return Response('<ul><li><table border="1"><tr><th>name</th><td>Test</td></tr><tr><th>')
如有帮助请点个赞
告诉Django一个字符串是安全的,不会自动转义:
{{ value|safe }}
我在 django.utils 下找到了一个叫做 format_html 的东西。
所以代码看起来像
render(request, 'home.html', {'value': format_html(json2html.convert(json=res))})
谢谢