How to add, multiply number variables in a Django template?

django-templates, for-loop, tags

Solution

There is the filter add from the documentation.

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.

In your case, you should store your counter in a JavaScript variable, and use it in your snippet.

Problem

The JS snippet I created relies on the forloop.counter variable being available within a `{% for key, value in data.items %}..{% endfor %}` tag. Is there a way to use mathematical operators (*, -, +, /) together with the `forloop.counter` variable?

Original source