我需要用flask渲染一个html模板。 我有一个 python 字典,我想在 TreeView 中显示它。 我该怎么做?
我找到了这个资源: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"
}
}
渲染模板的flask函数
@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?
最佳答案
您可以使用tojson
。
...
<div class="container">
<div id="tree"></div>
<script>
$(function() {
var mytree = JSON.parse('{{ res|tojson }}');
$('#tree').treeview({
data: mytree
});
});
</script>
...
关于javascript - 如何使用 python json 将模板渲染为 TreeView ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55345443/