Quick Django Tip – Modulus
Posted by Shane O'Sullivan on September 27, 2008
I was fiddling around with Dojo‘s Django Templating language (amazingly cool bit of tech of course), and got a bit stuck trying to do something very simple: modulus
All I wanted to do was stripe a list of elements by applying a different class based on whether a row was odd or even. It turns out the reason the answer didn’t really pop up in searches was because, in Django land, it is called divisibleby
So, if you are using a for loop in Django, to check if
(current loop index % 2) == 0
you can use
which returns either true or false.