使用JavaScript变量进行django模板列表查找

时间:2020-12-11 20:22:19

I've used js vars before with django templates, and it has worked, but this case is a bit different.

我之前使用过django模板的js变量,它已经有效,但是这种情况有点不同。

I need to use js var as a list index number to look up objects from a passed list in a django template var.

我需要使用js var作为列表索引号来查找django模板var中传递列表中的对象。

var index = 0;
$(.someclass).html (
    {{ list.index.some_attr }}
}

This does not work, and what's weird is I tested this:

这不起作用,我测试的是什么奇怪:

$(.someclass).html (
     {{ list.0.some_attr }}
 )

And it works!, This also works:

它有效!,这也有效:

var index = 0;
$(.someclass).html (
     index
}

This is a bit confusing, can anyone give an explanation? Thanks!

这有点令人困惑,任何人都可以解释一下吗?谢谢!

1 个解决方案

#1


0  

You cant access an iterable's member attribute without iterating it or pointing the member by index. This is quite normal in OOP. That said what is the problem you are trying to solve?

您无法访问迭代的成员属性而无需迭代它或通过索引指向成员。这在OOP中非常正常。那说你想解决的问题是什么?

#1


0  

You cant access an iterable's member attribute without iterating it or pointing the member by index. This is quite normal in OOP. That said what is the problem you are trying to solve?

您无法访问迭代的成员属性而无需迭代它或通过索引指向成员。这在OOP中非常正常。那说你想解决的问题是什么?