Using forloop.counter value as list index in a Django template Using forloop.counter value as list index in a Django template django django

Using forloop.counter value as list index in a Django template


I solved this in a rather inefficient way. Please don't throw up on your computer when you read this code. Given two lists of identical length, it will iterate through the first and print the corresponding item from the second.

If you must use this, only use it for rarely-accessed templates where the length of both lists will be small. Ideally, refactor your template's data to avoid this problem altogether.

{% for list1item in list1 %}   {% for list2item in list2 %}      {% if forloop.counter == forloop.parentloop.counter %}          {{ list1item }} {{ list2item }}      {% endif %}   {% endfor %}{% endfor %}


You can't use variables for attribute names, dictionary keys or list indices.

Also range(0,len(data)-1] is not valid python. It should be range(len(data)).

You probably don't need cycle. Maybe what you want is this:

{% for itemlist in data %}    ...    <table>        {% for item in itemlist %}        <tr>          <td>{{ item.a }}</td>          <td>{{ item.b }} ... </td>        </tr>        ...        {% endfor %}    </table>    {% if not forloop.last %}        <div class="page_break_div">    {% endif %}{% endfor %}


I wanted to have alternating colours in my table using a style sheet, by passing a list of toggling True/False values. I found this really frustrating. In the end I created a list of dictionary items with the same keys as the fields in the table, plus one more with the toggling true/false value.

def jobListView(request):    # django does not allow you to append stuff to the job identity, neither    # will it allow forloop.counter to index another list. The only solution    # is to have the toggle embedded in a dictionary along with    # every field from the job    j                   = job.objects.order_by('-priority')    # have a toggling true/false list for alternating colours in the table    theTog              = True    jobList             = []     for i in j:        myJob           = {}        myJob['id']     = i.id        myJob['duty']   = i.duty        myJob['updated'] = i.updated        myJob['priority'] = i.priority        myJob['description'] = i.description        myJob['toggle'] = theTog        jobList.append(myJob)        theTog          = not(theTog)    # next i    return render_to_response('index.html', locals())# end jobDetaiView

and my template

{% if jobList %}    <table border="1"><tr>    <th>Job ID</th><th>Duty</th><th>Updated</th><th>Priority</th><th>Description</th>    </tr>    {% for myJob in jobList %}        <!-- only show jobs that are not closed and have a positive priority. -->        {% if myJob.priority and not myJob.closeDate %}            <!-- alternate colours with the classes defined in the style sheet -->            {% if myJob.toggle %}                <tr class=d1>             {% else %}                <tr class=d0>            {% endif %}            <td><a href="/jobs/{{ myJob.id }}/">{{ myJob.id }}</td><td>{{ myJob.duty }}</td>             <td>{{ myJob.updated }}</td><td>{{ myJob.priority }}</td>            <td class=middle>{{ myJob.description }}</td>            </tr>        {% endif %}    {% endfor %}    </ul>{% else %}    <p>No jobs are in the system.</p>{% endif %}