Ever needed to compare some variable (that may come from db, forms, etc) in django templates with an integer?
Example:
1 2 3 4 5 |
{% if some_var == 3 %} working {% endif %} |
The above example will not work. Django’s template engine interprets the variable as a string.
Workaround for integer comparison:
1 2 3 4 5 |
{% if some_var|add:0 == 3 %} working {% endif %} |
By using the “add” filter, the variable is transformed into an integer.