python - Django - How to access elements in list by parent loop counter in template -
i have code
{% time in listoftimes %} {% booking in someotherlist.forloop.parentloop.counter0 %} {{ booking }} {% endfor %} {% endfor %} the booking variable not printed. think because cannot access someotherlist using forloop counter. how booking value?
assuming data follows:
listoftimes = ['time1', 'time2'] someotherlist = [['booking1', 'booking2'], ['booking3', 'booking4']] then in template can this:
{% time in listoftimes %} {% booking in someotherlist|get_index:forloop.counter0 %} {{ booking }} {% endfor %} {% endfor %} notice get_index filter in above code, need write custom filter in app templatetags:
from django import template register = template.library() @register.filter def get_index(l, i): return l[i] note: 2 list should of same size, otherwise indexerror might raised.
Comments
Post a Comment