简体   繁体   中英

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:

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!

Missing .items

Change

{% for i,v in dest %}

to

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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