'How can increment a variable value in django template language

How can I increment a variable value in django template language?

{% with a=0 %}
{% with b=4 %}
{% endwith %}
{% endwith %}

I used above code to assign value to a variable. I want to increment it by 4.



Solution 1:[1]

Jinja is similar to the Django template language, but they are not the same. Checkout the wikipedia

Jinja is a web template engine for the Python programming language. It was created by Armin Ronacher and is licensed under a BSD License. Jinja is similar to the Django template engine but provides Python-like expressions while ensuring that the templates are evaluated in a sandbox. It is a text-based template language and thus can be used to generate any markup as well as source code.

Now to your question, the following is the correct syntax for what I think you're trying to do. However, the django template is not the best place to do much math.

{% with a=0 %}
  {{ a|add:'4' }}
{% endwith %}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 raphael