【发布时间】:2020-05-12 16:57:55
【问题描述】:
我从视图中返回一个列表,我想在模板中按键输出具体的值。
返回列表(list_)
[
{'category 1': 1},
{'category 2': 3},
{'category 3': 4},
]
在模板中:
{{ list_.2.category 3}}
返回 4。
可以让模板标签更简单,直接按键输出吗?
例如:
{{ list_.key['category 3'] }}
我的看法:
def MapView(request):
applications = Application.objects.values(
'name', 'id', 'icon_name').filter(organization_id=1).order_by('name')
devices = Device.objects.all()
count_list = []
for a in applications:
count_num = devices.filter(id=a['id']).count()
count_list.append({
a['name']: count_num
})
context = {
'test': count_list,
}
return render(request, 'applications/map.html', context)
【问题讨论】:
标签: python django django-templates