迭代django模板中的json元素

时间:2022-02-03 20:20:00

This question has been asked multiple times and answered, but this simply doesn't work for me. My json structure is:

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

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

I return a json response from my django view as:

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

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

I am able to see the response object in my configuration.html as:

我能够在configuration.html中看到响应对象:

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

my problem is I'm not able to iterate through the remaining. I want app.id, app.finalStatus. I cannot access jobs.apps.app.id. I'm only able to access until app and not any further.

我的问题是我无法迭代剩下的。我想要app.id,app.finalStatus。我无法访问jobs.apps.app.id.我只能访问应用程序,而不是任何进一步。

Can anyone please please help me out?

有谁可以请帮帮我吗?

1 个解决方案

#1


1  

Your app contains a list of dict, so you need to loop on the list and get each dict:

你的应用程序包含一个dict列表,所以你需要在列表上循环并获取每个dict:

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

#1


1  

Your app contains a list of dict, so you need to loop on the list and get each dict:

你的应用程序包含一个dict列表,所以你需要在列表上循环并获取每个dict:

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