我有以下内容:
In [3]: Model.objects.get(id=123).field
Out[3]:
{u'1': None,
u'2': u'label 1',
u'3': u'label 2',
u'4': u'label 3',
u'5': None,
u'6': u'label 4',
u'7': None,
u'8': u'label 5',
}
我有一个带有以下模板加载的视图:
mapping = Model.objects.get(id=123).field
return render(request, 'final_application.html', {'predictions':mapping,...})
但是当我尝试在我的模板中迭代它时,它只包含值为None的JSON。这是我试过的代码
function run(){
{% for tag_id, suggestion in predictions.items %}
alert(tag_id); // just to display how it does not go through the whole JSON for some reason
//do the stuff I want to
{% endfor %}
}
此功能通过以下HTML
运行<button id="accept" onclick="run();">accept</button>
此警报1,然后是5,然后是7,这是唯一值为None的标记。为什么会发生这种情况而不是迭代整个JSON呢?有什么我想念的吗?
因为人们想知道for循环中还运行了什么,所以我用除了alert
注释掉的所有内容测试了代码,它的行为方式相同。这表明循环中的内容是无关紧要的。请注意,我也不允许在循环内部披露信息。