簡體   English   中英

如何使用 python json 作為樹視圖呈現模板?

[英]How to render template with python json as treeview?

我需要用 Flask 渲染一個 html 模板。 我在 python 中有一個 dict,我想在樹視圖中顯示它。 我該怎么做?

我發現這個資源: 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?

您可以使用tojson

...


<div class="container">
    <div id="tree"></div>

<script>
    $(function() {
  var mytree = JSON.parse('{{ res|tojson }}');

  $('#tree').treeview({
    data: mytree
  });
});
</script>


...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM