【问题标题】:I want to print the json object in a tabular form passed using render_template() from python我想以使用 python 中的 render_template() 传递的表格形式打印 json 对象
【发布时间】:2016-09-26 15:50:48
【问题描述】:

Python代码如下:

   @app.route("/send", methods=['GET', 'POST'])
    def send():
        if request.method == "POST":
            findemail=request.form['email']
            datafound=findlogic(findemail)
            data = jsonify(datafound)
            #return data
            return render_template("testjinja.html", x=data)

数据的格式

"629513533": [
        {
          "xyz": "629513533"
        }, 
        {
          "a": "1.00"
        }, 
        {
          "b": "3.00"
        }, 
        {
          "c": "1.00"
        }, 
        {
          "d": "1.00"
        }, 
        {
          "e": "1.00"
        }, 
        {
          "f": "1.00"
        }, 
        {
          "g": "1.00"
        }, 
        {
          "h": "1.00"
        }, 
        {
          "i": "1.00"
        }, 

我试过 testjinja.html 如下:

<body>
{% for value in x %}           // I have tried x.iteritems(), x.items() also 
  <li>{{ x[value].xyz }} </li>
{% endfor %}
</body>

我收到响应对象不可迭代的错误。我不知道如何处理 testjinja.html 中的 json 对象 x。请帮忙

【问题讨论】:

    标签: python html json


    【解决方案1】:

    你不能像你想要的那样迭代一个字符串,但看起来你的 data 是一个 json 字符串,所以你可以这样做:

    json_data = json.loads(data)
    
    <body>
    {% for value in json_data['629513533'] %}           
      <li>{{ x[value].xyz }} </li>
    {% endfor %}
    </body>
    

    你必须import json

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题,我通过在 py 端创建一个数组并将其通过渲染传递给我的模板来解决。

      例子:

      status = {'summary': data["status"], 'date_applied': data["date_app"], 'message': data["message"], 'location': data["location"], 'title': data["title"]}       
      return render.operatingStatus(status)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-10-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-02-15
        • 2012-09-12
        • 2021-03-06
        相关资源
        最近更新 更多