Using enlarged / reduced variables in django templates

Maybe this is a bit silly question, but I did not find the answer. Is there a way to use incremented / decremented variables in django templates?

eg {{ some_variable + 1 }}

+5
source share
3 answers

There is a filter built in add:

{{ some_variable|add:"1" }}
+10
source

One way to do this is to use the django template filter.

https://docs.djangoproject.com/en/dev/howto/custom-template-tags/#writing-custom-template-filters

def inc(value):
    return value+1

and then:

{{ some_variable|inc }}
+1
source

forloop.counter, .

{% for a in object_list %}
{{ forloop.counter }}
{% endfor %}
+1

All Articles