简体   繁体   English

Javascript无法读取Django模板中字典的键和值

[英]Javascript not reading the keys and values of a dictionary in django template

I have a dictionary like: 我有一本字典,例如:

dest = {2: [u'canada', u'A', 'Ottawa'], 5: [u'Malaysia', u'A', 'KualaLumpur'],...}

Then I tried to retrieve the keys and values from dest in django template using javascript: 然后,我尝试使用javascript从django模板中的dest检索键和值:

function categorizeReports()
{
      var a = [];
      var b = [];
      {% for i,v in dest %}
        id = '{{i}}';
        console.log('id', id)
        values = '{{v}}';
        console.log('values',values)
        {% for name, type, cat in values %}
            if(type=='A' && cat=='Ottawa')
                {
                     a.push(id,name,type,cat)
                }
            if(type=='A' && cat=='KualaLumpur')
                {
                     b.push(id,name,type,cat)
                }

        {% endfor %}
      {% endfor %}
      console.log(a)
      console.log(b)
 }

But both the Arrays are shown as 'an empty string' as well as id and values are too shown as 'an empty string' , Please help! 但是两个数组都显示为'an empty string' ,id和值也显示为'an empty string' ,请帮忙!

Missing .items 丢失物品

Change 更改

{% for i,v in dest %}

to

{% for i,v in dest.items %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM