python - to perform addition operation on forloop.counter in django template -


i want perform reduce value of forloop.counter in django template given condition, possible in django.

below demonstrated example

{% in item %}     {% if forloop.counter0|divisibleby:4 %}         start     {% endif %}         {% if %}             item{{ forloop.counter }}         {% else %}             ######### here want reduce value of forloop.counter 1 ###########         {% endif %}     {% if forloop.counter|divisibleby:4 %}         end     {% endif %}  {% endfor %}  

in above code 8 perfect item output be

start item1 item2 item3 item4 end start item5 item6 item7 item8 end 

but suppose item2 none, output

start item1  item3 item4 end start item5 item6 item7 item8 end 

i want print in form of proper ascending order (incremented 1 @ each step) reducing value of forloop each time if condition not satisfied. please don't suggest custom template tag, know , consider last option.

i doubt django let mess forloop.counter easily, , wouldn't mess anyway. obvious solution filter out list before iterate on it, can done in view or (if insist on doing in template) using custom filter.

or can wrap list in generator function take care of filtering , numbering, ie:

def filteriternum(seq):     num = 0     item in seq:         if not item:             continue         num += 1         yield num, item 

here again, can either wrapping in view or write custom template filter of tag wrapping.


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -