Anand Aron
Anand Aron

Reputation: 11

Django loading json objects to templates

I am getting data from firebase

views.py

def posts (request):
     _posts= ref.child('posts').get();  #getting data from firebase
     #_posts={u'1536855154789': 
     #      {u'content': u'Yahoo! The sports day is near!!', 
     #       u'image': u'https://firebasestorage.googleapis.com/.../img_1536855147507.jpeg?', 
     #       u'title': u'Sports Day'}, 
     #u'-LMJBAc3iZRklICAwDC1': 
     #     {u'content': u'Find the chef in your child. Food fest is here!!', 
     #      u'image': u'https://firebasestorage.googleapis.com/.../img_1536863277649.jpeg?', 
     #      u'title': u'Food Fest '}, 
     #u'-LNaLKKqSIZHraf3oedT': 
     #     {u'content': u'Exploring is part of education. Hence a tour to Historical and Cultural Heritage monuments in Delhi has been planned.',
     #      u'image': u'https://firebasestorage.googleapis.com/.../img_1538241669221.jpeg', 
     #      u'title': u'Educational Tour'}}
     return render(request, 'post.html', {'postList': _posts})

index.html

 {% for item in postList %}
 {{ item.title}}</br>
 {% endfor %}

Its returning nothing

my firebase data https://i.sstatic.net/lxYsC.png

Help me solve this. Thanks in advance

Upvotes: 0

Views: 86

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599778

Your code would work if you had a list of dicts. But you don't; you have a single dictionary, whose keys are the IDs and the values are themselves dicts. So you need to iterate over the dict values:

 {% for item in postList.values %}
 {{ item.title}}</br>
 {% endfor %}

Upvotes: 1

Related Questions