-2

Code i used in fetching the data

url = 'https://api.some-random-url.com/users/amarlearning'
serialized_data = urllib2.urlopen(url).read().decode("utf-8")
data = json.loads(serialized_data)

Returned JSON

[
   {
       id: "4859421681",
       type: "IssueCommentEvent"
   },
   {
       id: "4859421681",
       type: "IssueCommentEvent"
   },
   {
       id: "4859421681",
       type: "IssueCommentEvent"
   }
]

Now In django Template i am doing this

{{ data[0]['type'] }}

Getting Template error, while i was accessing Json data in Django template!

Amar Prakash Pandey
  • 1,278
  • 18
  • 23

2 Answers2

2

Documentation says -

Dictionary lookup, attribute lookup and list-index lookups are implemented with a dot notation

So you can access the data via -

data.0.type
Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
Md. Al-Amin
  • 1,423
  • 1
  • 13
  • 26
1

Syntax in Django templates are different from the plain python. You have to do:

data.0.type

For details also check:


In case you want to print all the items within the JSON, loop in your template file should be like:

{% for d in data %}
    {% for key in d %}
        {{d.key}}
    {% endfor %} 
{% endfor %}
Community
  • 1
  • 1
Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126