hermy67
hermy67

Reputation: 138

iterate through json elements in django template

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

{
   "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:

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:

<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.

Can anyone please please help me out?

Upvotes: 1

Views: 401

Answers (1)

Shang Wang
Shang Wang

Reputation: 25539

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

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

Upvotes: 1

Related Questions