我试图在一个嵌套的上下文字典中循环,在其中我遗漏了一些东西,而我正在使用的字典是,
output =
{
'results': {
'result2': {
'name': 'Alex',
'roll': 5,
'phone': 'not provided',
'email': '[email protected]',
'grade': 8,
},
'result0': {
'name': 'John',
'roll': 23,
'phone': 'not provided',
'email': '[email protected]',
'grade': 8,
},
'result1': {
'name': 'Mike',
'roll': 35,
'phone': 'not provided',
'email': '[email protected]',
'grade': 8,
}
},
'status' : 'ok',
}
一个简单的循环,
{% for result in results %}
{{ result }}
{% endfor %}
当结果为result2
,result0
和result1
,
更改结果以{{result.name}}
访问值,则不会呈现任何内容。
如何使用键渲染值?
谢谢。
results
是一本字典,遍历字典可为您提供键。要遍历这些值,请明确地说:
{% for result in results.values %}
{{ result }}
{% endfor %}
如果需要键和值,也可以使用以下命令进行操作items
:
{% for key, value in results.items %}
{# ... #}
{% endfor %}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句