【问题标题】:iterate through json elements in django template遍历 django 模板中的 json 元素
【发布时间】:2016-08-13 15:47:09
【问题描述】:

这个问题已经被问过多次并得到了回答,但这对我来说根本不起作用。 我的json结构是:

{
   "apps": {
      "app": [
         {
            "logAggregationStatus": "SUCCEEDED", 
            "runningContainers": -1, 
            "allocatedVCores": -1, 
            "clusterId": 234234, 
            "amContainerLogs": "url", 
            "id": "1",  
            "finalStatus": "SUCCEEDED"
         }
      ]
   }
}

我从我的 django 视图返回一个 json 响应:

def configuration(request):
 response = requests.get("http://my_url/") 
 job = response.json()
 return render_to_response("configuration.html", {"job": job})

我可以在我的 configuration.html 中看到响应对象:

<tr>
  {% for app in job %}
    {{ job.apps.app }}
  {% endfor %}
</tr>

我的问题是我无法遍历其余部分。我想要 app.id、app.finalStatus。我无法访问jobs.apps.app.id。我只能访问直到应用程序,而不能再访问。

谁能帮帮我?

【问题讨论】:

    标签: javascript arrays json django django-templates


    【解决方案1】:

    您的app 包含一个字典列表,因此您需要在列表上循环并获取每个字典:

    <tr>
      {% for app in job.apps.app %}
        {{ app.id }}
        {{ app.finalStatus }}
      {% endfor %}
    </tr>
    

    【讨论】:

      猜你喜欢
      • 2017-08-13
      • 2018-02-24
      • 1970-01-01
      • 2011-07-16
      • 2018-05-24
      • 2016-11-12
      • 2019-11-24
      • 2010-10-21
      • 1970-01-01
      相关资源
      最近更新 更多