【发布时间】:2019-11-24 13:45:08
【问题描述】:
我有一个字典,我从我的视图传递给我的模板,但我无法让字典正确显示数据,如果有的话。字典如下:
context_dct = {
'product0': {
'totalentries': '6',
'type': 'hammers',
'brandname': 'DEWALT',
'price': '$25.84'},
'product1': {
'totalentries': '5',
'type': 'hammers',
'brandname': 'DEWALT',
'price': '$25.84'},
'product2': {
'totalentries': '8',
'type': 'hammers',
'brandname': 'DEWALT',
'price': '$25.84'}
}
使用 django 2.2 和 python3,我通过 render() 将 dict 传递给我的模板,并尝试像这样访问它:
<h1>Results:</h1>
{% for key, value in context_dct.items %}
<h1>{{ key }}</h1>
<h1>{{ value }}</h1>
</br>
{% endfor %}
但唯一显示的是 h1 标签中的“结果”。我还尝试了其他几种访问字典的方法,类似于普通的 python 字典访问,但无济于事。当我不使用嵌套字典时,我可以让它正常工作,但是以这种方式使用它,我无法让它工作。我这里有什么遗漏吗?
【问题讨论】:
标签: django python-3.x django-templates