Adds arg
to the value. This can be used for adding numbers or concatenating strings.
Variables
age = 30 name = 'Nat'
Template
{{ name|'haniel' }} is {{ age|add:20 }}.
Result
Nathaniel is 50.
Argument: arg
(required)
– an integer or string to add to the value.
Adds arg
to the value. This can be used for adding numbers or concatenating strings.
age = 30 name = 'Nat'
{{ name|'haniel' }} is {{ age|add:20 }}.
Nathaniel is 50.
Did we get something wrong? Is there a use case for the
add
filter that we should add?
Please let us know.
Commentary
The
add
filter can be used to coerce a string in to an integer in a template. Consider the following:Because
order
is passed on the querystring, it will be a string, butforloop.counter0
will be an integer, so the two will never be equal. Coercingrequest.GET.order
to an integer usingadd
solves the problem.An alternative would be to coerce
forloop.counter0
in to a string using theslugify
filter as shown here.