0

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 parentloopid %}
           {% with forloop.counter as childloopid %}
        <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:even 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 :)

djvg
  • 11,722
  • 5
  • 72
  • 103
stackover
  • 6,275
  • 6
  • 22
  • 20

2 Answers2

0

For numbering the rows you can use the forloop.counter Here you can see an example how to use it.

To switch between even and odd you can use the cycle template tag

{% for project in object_list %}

<tr class="{% cycle odd,even %}">  ... </tr>

{% endfor %}
Thomas Kremmel
  • 14,575
  • 26
  • 108
  • 177
  • Tom forloop itself is good if i have had a single for loop. here as am using nested forloop.so the inner loops counter0 is always set to 0 when outerloop is increased. I'll tyr cycle and see .Thanks :) – stackover Jan 07 '11 at 19:13
0

answer for my question:

  1. Numbering would be a bit time taking. one option is to design custom filters or other way is to modify views and use simple forloop.counter to add, count and forloop.counter. Let me give an example: for the above cases, results are sorted dictionaries with grades and students,something like this ((A:a,b,c,d,e), (B:f,g,h,i), (C:j,k,l,m,n)). In the view add one more dictionary to each tuple with the student count of previous tuple.

    temp_count = 0  
    for tup in results:  
        tup[1].append({'count':temp_count})  
        temp_count = temp_count + len(tup[1])-1
    

    -1 is because we don't want to avoid dictionary counted Inside the template

    {% with tup|last|last as cnt %}  
        {% with forloop.counter as rnum %}  
            {% with rnum|add:cnt.count as rownum %}  
                <td>{{rownum}}</td>
    
                rest of code goes here 
    
            {% endwith %}
        {%endwith%}
    {% endwith %}
    
  2. using {% cycle %} won't be that helpful when using nested loops,

    <tr class="{% if rownum|divisibleby:2 %}"even"{% else %}"odd"{% endif %}">
    

    follow this clean way to color.

djvg
  • 11,722
  • 5
  • 72
  • 103
stackover
  • 6,275
  • 6
  • 22
  • 20