Javascript不读取django模板中字典的键和值

标签 javascript python django django-templates

我有一本字典,例如:

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

然后我尝试从 dest 检索键和值在 django 模板中使用 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)
 }

但这两个数组都显示为 'an empty string' id 和值也显示为 'an empty string' ,请帮忙!

最佳答案

缺少.items

改变

{% for i,v in dest %}

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

关于Javascript不读取django模板中字典的键和值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16861866/

相关文章:

javascript - jQuery 和触摸/移动 : Perform Function on Swipe Left/Right

javascript - 我的游戏存在性能泄漏,但我找不到它

python 不能从静态方法调用模块方法?

django - 如何在 Django 管理表单验证器中检测更新

javascript - ngInit 无法与 AngularJS 和 Django 一起正常工作

javascript - AngularJS:无法检索 JSON 文件

javascript - 如何读取 VueJs 中元素的 data-*?

python - pybind11: AttributeError: 尝试从 py 文件运行函数时,模块 'XXX' 没有属性 'YYY'

python - Python,Powershell和Popen

django - 如何在 uWSGI 中配置允许的 GET 参数字符数?