我试图用HTML求和,但模板标记返回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)
我在这里做错什么了?
你能帮我一下,如何用模板标签返回总数?
我怀疑你的列表是一个迭代器。所以第一次迭代,第二次什么都不迭代。 所以你应该这样做
在调用模板之前
您的模板标记看起来不正确。您使用
role_total
作为参数,然后遍历list_total
(似乎未定义),并从列表中的每个字典中尝试获取也似乎未定义的键list_sum
。从模板中调用它作为
<td>{{ list|running_total}}</td>
相关问题 更多 >
编程相关推荐