问题描述
我创建的JS代码段依赖于一个中的forloop.counter变量,对于key,在data.items%}中的值为{%endfor%}
标签。
The JS snippet I created relies on the forloop.counter variable being available within a {% for key, value in data.items %}..{% endfor %}
tag.
有没有办法使用数学运算符(*, - ,+,/)和 forloop.counter
变量?
Is there a way to use mathematical operators (*, -, +, /) together with the forloop.counter
variable?
推荐答案
有一个过滤器 从文档。
There is the filter add from the documentation.
我很确定没有内置的方式在Django模板中使用其他数学运算。您可以随时然而。这样做并不总是一个好主意。
I'm pretty sure there are no built-in way to use the other mathematical operations over numbers in Django templates. You can always make your own however. It is not always a good idea to do so.
您希望将视图中的逻辑保持在模板中,并将渲染保留在模板中。
You want to keep your logic inside the views and keep the rendering inside the templates.
在您的情况下,您应该将计数器存储在JavaScript变量中,并在您的代码段中使用它。
In your case, you should store your counter in a JavaScript variable, and use it in your snippet.
这篇关于如何在Django模板中添加多个数字变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!