我需要用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)
test.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?
答案 0 :(得分:1)
您可以使用tojson
。
...
<div class="container">
<div id="tree"></div>
<script>
$(function() {
var mytree = JSON.parse('{{ res|tojson }}');
$('#tree').treeview({
data: mytree
});
});
</script>
...