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?
Another approach to declare variables in the template is by using custom template tags. Create a custom template tag files named as custom_template_tags.py . Paste the below code in it. Now inside the HTML template use this custom template tag setvar to define a new variable.
It's possible to use django built-in widthratio template tag and add filter:
{{forloop.counter|add:5}}
{{forloop.counter|add:"-5"}}
{% widthratio forloop.counter 5 1 %}
{% widthratio forloop.counter 1 5 %}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With