On Sun, Oct 23, 2011 at 12:37 PM, Tsung-Hsien <jasoniem9...@gmail.com> wrote:
> I want to filter a loop when the number comes to 1,5,9,13,17...
> I use this but fail
> {% if forloop.counter |divisibleby range(1,100,4)%}<tr>{% endif %}
> How to fix it, thanks a lot !!

Firstly, your stated problem definition -- identifying when a loop
hits iteration 1, 5, 9, 13... -- isn't the same as what you've tried
to encode. What you've encoded is "if the number is divisible by
1,5,9,13,...". For example, 10 is divisible by 5, but isn't in the
sequence 1,5,9,13,...

Secondly, the reason this code doesn't work is that you can't use
arbitrary Python expressions in a Django template. range() isn't a
valid Django template operator -- in fact, almost nothing which
requires calling with brackets will be.

The good news is that what you're asking for is really easy to do: you
use the modulus operator.

{% if forloop.counter % 4 == 1 %}

i.e., Take get the modulus (remainder) after dividing the counter by
4; if the remainder is 1, the condition is true.

Yours,
Russ Magee %-)

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to