如何在 html django 中聚合
编写了一个excel格式的html指标来表示日期明智的计数
def get_detail(usr,dt):
res = Dataset.objects.filter(user = usr, date = dt).values('expense')
try :
output=res[0]['expense']
except IndexError:
output=" "
return output
上面的代码是我生成的表及其工作的函数
def metrics_new(request, year = None, month = None):
import calendar
from datetime import *
from dateutil.relativedelta import relativedelta
m = Profile.objects.filter(lead = 'sushanth' ,status='A')
now=datetime.today()
if not year:
year = now.year
if not month:
month = now.month
year = int(year)
month = int(month)
d = calendar.mdays[month]
out_txt="<TABLE id=\"myTable\" class=\"tablesorter\">\n"
out_txt += "<THEAD>"
out_txt += "<TR>"
out_txt += "<TH> LDAP </TH>"
for i in xrange(1,d):
date_format = now.replace( year = year , month = month , day = i )
out_txt += "<TH>" + str(date_format) + "</TH>"
out_txt += "</TR>\n"
out_txt += "</THEAD>"
s_no = 0
for fetch in m:
out_txt += "<TR>"
ld = fetch.user
out_txt += "<TD>" + str(ld) + "</TD>"
for i in xrange(1,d):
date_format = now.replace( year = year , month = month , day = i )
out_txt += "<TD>" + str(get_details(ld,date_format)) + "</TD>"
out_txt += "<TR>\n"
out_txt +="</TABLE>\n"
try:
cal_date = date(int(year), int(month), 1)
except ValueError:
raise Http404
prev_month = (cal_date + relativedelta(months = -1))
next_month = (cal_date + relativedelta(months = +1))
return render_to_response('metrics.html', {'table':out_txt,
'prev_month':prev_month,'next_month': next_month, },
context_instance = RequestContext(request))
,如何聚合每行
输出的费用:
user 2011-02-01 2011-02-02 2011-02-03 2011-02-04.......2011-02-28
x 2 4 5 2
y 1 2
z 4
但我想要以下格式的输出
user 2011-02-01 2011-02-02 2011-02-03 2011-02-04.......2011-02-28 total
x 2 4 5 2 13
y 1 2 3
z 4 4
这里我想要将数据汇总在 结束?
有什么最好的方法可以做到这一点吗?
提前致谢
Wrote a excel format html metrics to represent the date wise counts
def get_detail(usr,dt):
res = Dataset.objects.filter(user = usr, date = dt).values('expense')
try :
output=res[0]['expense']
except IndexError:
output=" "
return output
above code is a function
def metrics_new(request, year = None, month = None):
import calendar
from datetime import *
from dateutil.relativedelta import relativedelta
m = Profile.objects.filter(lead = 'sushanth' ,status='A')
now=datetime.today()
if not year:
year = now.year
if not month:
month = now.month
year = int(year)
month = int(month)
d = calendar.mdays[month]
out_txt="<TABLE id=\"myTable\" class=\"tablesorter\">\n"
out_txt += "<THEAD>"
out_txt += "<TR>"
out_txt += "<TH> LDAP </TH>"
for i in xrange(1,d):
date_format = now.replace( year = year , month = month , day = i )
out_txt += "<TH>" + str(date_format) + "</TH>"
out_txt += "</TR>\n"
out_txt += "</THEAD>"
s_no = 0
for fetch in m:
out_txt += "<TR>"
ld = fetch.user
out_txt += "<TD>" + str(ld) + "</TD>"
for i in xrange(1,d):
date_format = now.replace( year = year , month = month , day = i )
out_txt += "<TD>" + str(get_details(ld,date_format)) + "</TD>"
out_txt += "<TR>\n"
out_txt +="</TABLE>\n"
try:
cal_date = date(int(year), int(month), 1)
except ValueError:
raise Http404
prev_month = (cal_date + relativedelta(months = -1))
next_month = (cal_date + relativedelta(months = +1))
return render_to_response('metrics.html', {'table':out_txt,
'prev_month':prev_month,'next_month': next_month, },
context_instance = RequestContext(request))
I have generated the table and its working,how to aggregate the expenses at each row
output :
user 2011-02-01 2011-02-02 2011-02-03 2011-02-04.......2011-02-28
x 2 4 5 2
y 1 2
z 4
but i want the out in below format
user 2011-02-01 2011-02-02 2011-02-03 2011-02-04.......2011-02-28 total
x 2 4 5 2 13
y 1 2 3
z 4 4
here i want to aggregate the data at
the end ?is there any best way i can do this ?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从视图中删除所有 HTML 创建代码。
视图需要为模板创建所需的变量,并且模板应该处理格式设置。也许创建一个用户列表,并为每个用户创建一个长度为 calendar.mdays[month] 的列表,其中包含该用户的范围和一个 sum 变量。
当您在视图中执行此操作,并且有每个用户的费用列表时,计算总和应该像这样简单
那么您的模板代码应该很简单:
Remove all the HTML creating code from the view.
The view needs to create the needed variables for the template, and the template should deal with formatting. Perhaps create a list of users, and for each user, a list of length calendar.mdays[month], containing the expanses of this user, and a sum variable.
when you do it in the view, and have a list of expenses for each user, calculating the sum should be as easy as
Then your template code should be simple: