how to number t-rows ,when table generated using nested forloop in django templates
- by stackover
Hi,
This part is from views.py
results=[(A,[stuObj1,stuObj2,stuObj3]),(B,[stuObj4,stuObj5,stuObj6]),(C,[stuObj7,stuObj8])]
for tup in results:
total = tot+len(tup[1])
render_to_response(url,{'results':res , 'total':str(tot),})
this is template code:
<th class="name">Name</th>
<th class="id">Student ID</th>
<th class="grade">Grade</th>
{% for tup in results %}
{% for student in tup|last %}
{% with forloop.parentloop.counter as parentid%}
{% with forloop.counter as centerid%}
<tbody class="results-body">
<tr>
<td>{{student.fname|lower|capfirst}} {{student.lname|lower|capfirst}}</td>
<td>{{student.id}}</td>
<td>{{tup|first}}</td>
</tr>
{% endfor %}
{% endfor %}
Now the problems am having are 1. numbering the rows.
Here my problem is am not sure if i can do things like total=total-1 in the templates to get the
numbered rows like <td>{{total}}</td>
2.applying css to tr:ever or odd.
Whats happening in this case is everytime the loop is running the odd/even ordering is lost.
these seems related problems. Any ideas would be great :)