Django’s template tags include an {% ifchanged %} test for use within loops to check if a value has changed. I’m using it to output a new <div> tag every time a variable changes.
I want every 4th <div> to have a specific class, but Django’s forloop.counter variable isn’t helpful here as it increments every time the loop runs, even when the {% ifchanged %} test doesn’t run, so it outputs irregularly.
Does anyone know of a way to get around this? Obviously I can’t set my own counter inside a Django view, so I have no way of checking when the ‘inner’ loop has changed.
Thanks,
Matt
You can simply use the divisibleby filter:
Update:
You have to use a counter+
divisiblebyfilter in your template. Look at this template tag: Counter, it can help you.Or
Filter out duplicate items (if possible) in the view before passing them to the template and use the
divisiblebyfilter.