如何使用django模板从列表中获取dict值

时间:2012-05-03 05:50:24

标签: django django-templates

我的观点:

post_details = Post.objects.all()
    for each in post_details:
        post_like_list=PostLikes.objects.filter(post = each)
        pl=len(post_like_list)
        dict1={"post":each}
        dict2={"how many likes":pl}
        list.append([dict1,dict2])
    print list

我的列表= [[{'发布':住宿},{'有多少人喜欢':0}],[{'发布':选举},{'有多少人喜欢':0}],[{'发布':},{'有多少人喜欢':3}],[{'发布':},{'多少喜欢':1}]]

通过使用django模板,我怎样才能从列表中的字典中获得“发布”和“多少喜欢”?

1 个答案:

答案 0 :(得分:4)

如果你让结构健全......

    list.append({"post": each, "how many likes": pl})

然后您会发现可以使用{% for %}迭代列表,然后使用常规访问方法访问dicts。

相关问题