Python-Django: ifchanged template tag
Question: Here i am getting employee with duration from database.Same employee with 2 or
3 records. so gettting employee duration and adding and displaying,if employee
ID changed then again it calculate the employee duration and displaying I need
fo find each employee ID has how many records,through `{{ forloop.counter
}}`individually

Here my problems are...

  1. If my loop coming to `{% ifchanged eachSc.laEmpNum %}` then `{{ result.0 }}`. If again then `{{ result.1 }}` and `{{ result.2 }}` then so on...

  2. If my loop coming to `{% ifchanged eachSc.laEmpNum %}` then my `{{ forloop.counter }}` should start with 0(zero) again.

`result = [5.5, 4.5, 1.3]` which i am getting from `view.py`

    
    
    {% for eachSc in DBShots1 %}
    
    <tr>
        {% ifchanged eachSc.laEmpNum %} 
    
        <td bgcolor="#FFFACD" width="1%">Tot={{ result }}</td>
        {% endifchanged %}
    
    </tr>
        <td bgcolor="#FFFACD" width="1%">{{ forloop.counter }} </td> 
    
        <td bgcolor="#CCFACD" width="1%">{{ eachSc.sName }}</td>
    
        <td bgcolor="#CCF0F5" width="1%">{{ eachSc.duration }}</td>
    
        <td bgcolor="#CCFACD" width="1%">{{ eachSc.frames }}</td>
    
        <td bgcolor="#CCFACD" width="5%">{{ GetEmpDept }} - {{ getEmpName.emp_name }} - {{ eachSc.laEmpNum }}</td>
    {% endfor %}
    


Answer: I believe there are a couple of issues to deal with here. First of all I see
you want to print an item of the 'result' variable, depending on the position
of the forloop counter. This is not directly possible in django templates (for
various fair reasons). To quickly solve this without reorganizing your data in
the view, you can define a custom filter that simply returns a list item on
the specified index. You could put this in your templatetags/myfilters.py:

    
    
    from django import template
    register = template.Library()
    
    @register.filter
    def getitem(mylist, index):
        return mylist[index]
    

Then, to reset the forloop counter when 'laEmpNum' changes you should use the
'regroup' django template tag like this:

    
    
    {% load myfilters %}
    {% regroup DBShots1 by laEmpNum as eachScList %}
    {% for eachScGrp in eachScList %}
        {% for eachSc in eachScGrp.list %}
        <tr>
          <td bgcolor="#FFFACD" width="1%">{{ forloop.counter }} </td> 
          <td bgcolor="#CCFACD" width="1%">{{ eachSc.sName }}</td>
          <td bgcolor="#CCF0F5" width="1%">{{ eachSc.duration }}</td>
          <td bgcolor="#CCFACD" width="1%">{{ eachSc.frames }}</td>
          <td bgcolor="#CCFACD" width="5%">{{ GetEmpDept }} - {{ getEmpName.emp_name }} - {{ eachSc.laEmpNum }}</td>
         </tr>
        {% endfor %}
        <tr>
            <td> </td><td> </td>
            <td bgcolor="#FFFACD" width="1%">Tot={{ result|getitem:forloop.counter0 }}</td>
        </tr>
    {% endfor %}
    

The first line loads our custom filter library. The last part uses the custom
filter to retrieve a result item based on the iteration over the 'laEmpNum'
groupper.

