如何在 Django 模板中添加、乘以数字变量? [英] How to add, multiply number variables in a Django template?

查看:24
本文介绍了如何在 Django 模板中添加、乘以数字变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建的 JS 片段依赖于在 {% for key, value in data.items %}..{% endfor %} 标签中可用的 forloop.counter 变量.

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?

推荐答案

有过滤器 从文档中添加.

我很确定没有内置方法可以在 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 模板中添加、乘以数字变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆