使用模板标签在 html 模板中求和 [英] Sum in html template using template tag
本文介绍了使用模板标签在 html 模板中求和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图在 HTML 中求和,但模板标签返回 0 ,
I am trying to sum in HTML,but template tag return 0 ,
查看.py
def gen_Report(request):
### query returns below output
list=[{'total': 1744, 'user': u'x'}, {'total': 13, 'user': u'y'}, {'total': 126, 'user': u'z'}, {'total': 46, 'user': u'm'}, {'total': 4, 'user': u'n'}, {'total': 8, 'user': u'o'}, {'total': 3, 'user': u'p'}]
return render_to_response('user.html', locals(),
context_instance = RequestContext(request))
模板:
user.html
{% load temptags %}
<table id="myTable" class="tablesorter">
<thead>
<tr>
<th>S.No</th>
<th>role</th>
<th>Count</th>
</tr>
</thead>
{% for fetch in list %}
<tr>
<td>{{forloop.counter}}</td>
<td>{{fetch.user}}</td>
<td>{{fetch.total}}</td>
{% endfor %}
<td>{{ list.total|running_total}}</td>
<tr>
</table>
模板标签:
from django.template import Library
register = Library()
@register.filter
def running_total(list_total):
return sum(d.get('list_sum') for d in list_total)
输出:
S.No user Count
1 x 1744
2 y 13
3 z 126
4 m 46
5 n 4
6 o 8
Sum------------------> 0 (it returns zero)
我在这里做错了什么?
你能帮我吗,如何在这里使用模板标签返回总和?
can you please help me,how to return sum total using template tag here ?
推荐答案
您的模板标签看起来有误.您将 role_total
作为参数,然后遍历 list_total
(似乎未定义)并从列表中的每个字典尝试获取键 list_sum
也似乎未定义.
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)
并从模板调用它作为 <td>{{ list|running_total}}</td>
这篇关于使用模板标签在 html 模板中求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文