简体   繁体   English

Django将json对象加载到模板

[英]Django loading json objects to templates

I am getting data from firebase 我正在从Firebase获取数据

views.py 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 的index.html

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

Its returning nothing 它什么也没回报

my firebase data https://i.stack.imgur.com/lxYsC.png 我的Firebase数据https://i.stack.imgur.com/lxYsC.png

Help me solve this. 帮我解决这个问题。 Thanks in advance 提前致谢

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. 您只有一个字典,其键是ID,值本身是字典。 So you need to iterate over the dict values: 因此,您需要遍历dict值:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM