Sum in html template using template tag

后端 未结 2 1961
萌比男神i
萌比男神i 2020-12-21 16:36

I am trying to sum in HTML,but template tag return 0 ,

View.py

def gen_Report(request):

### query returns below output 
list=[{\'total\': 1744, \'us         


        
相关标签:
2条回答
  • 2020-12-21 16:54

    I suspect that your list is an iterator. So the first time it iterates over and the second time it iterate over nothing. So you should do something like this

    for d in list_total:
       d.set('list_sum', list(d.get('list_sum')))
    

    before calling your template

    0 讨论(0)
  • 2020-12-21 16:55

    Your template tag looks wrong. You have role_total as the parameter and then iterate through list_total (seemingly undefined) and from each dictionary in the list try getting the key list_sum which is also seemingly undefined.

    from django.template import Library
    register = Library()
    @register.filter
    def running_total(your_dict_list):
       return sum(d['total'] for d in your_dict_list)
    

    and calling it from the template at as <td>{{ list|running_total}}</td>

    0 讨论(0)
提交回复
热议问题