我需要用 Flask 渲染一个 html 模板。我在 python 中有一个字典,我想在树视图中显示它。我该怎么做?我发现这个资源:https : //github.com/jonmiles/bootstrap-treeview 可能有用,但我不能使用它。dict_var = { "test3@test3.com": { "discord_id": 0, "status_key": False, "username": "test3@test3.com", "last_name": "aaaa", "gender": "male", "email": "test3@test3.com", }, "test9@test9.com": { "username": "test9@test9.com", "last_name": "test9", "gender": "male", "discord_data": { "user": { "avatar": None, "discriminator": "111", "verified": True, }, "connections": [], "guilds": [ { "icon": "hellohellohellohello.jpg", "id": "222222222222", }, { "icon": None, "id": "111111111", "owner": True, } }, "email": "hello@hello.com", "activation_key": "hello-hello-hello-hello-hello", "first_name": "hello@hello.com", "discord_id": 22222223333334444, "state": "IT" } }渲染模板的烧瓶函数@app.route('/test')def test(): return render_template('test.html', res=dict_var)测试.html...<div class="container"> <div id="tree"></div><script> $(function() { var mytree = JSON.parse('{{ res|safe }}'); $('#tree').treeview({ data: mytree });});</script>...如何将res传递给js并将dict转换为treeview?
1 回答
陪伴而非守候
TA贡献1757条经验 获得超8个赞
您可以使用tojson.
...
<div class="container">
<div id="tree"></div>
<script>
$(function() {
var mytree = JSON.parse('{{ res|tojson }}');
$('#tree').treeview({
data: mytree
});
});
</script>
...
添加回答
举报
0/150
提交
取消