如何在 html django 中聚合

发布于 2024-10-20 11:33:27 字数 2592 浏览 2 评论 0原文

编写了一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

勿忘初心 2024-10-27 11:33:27

从视图中删除所有 HTML 创建代码。

视图需要为模板创建所需的变量,并且模板应该处理格式设置。也许创建一个用户列表,并为每个用户创建一个长度为 calendar.mdays[month] 的列表,其中包含该用户的范围和一个 sum 变量。

users_list = [{'username':'x', 'expense_list':[2,4,5,0,0,0,2], 'sum':13}, ...]

当您在视图中执行此操作,并且有每个用户的费用列表时,计算总和应该像这样简单

user['sum'] = sum(user['expense_list'])

那么您的模板代码应该很简单:

{% for user in users_list %}
    <tr>
    <td>{{ user.username }}</td>
    {% for expense in user.expense_list %}
        <td>{{ expense }}</td>
    {% endfor %}
    <td>{{ user.sum }}</td>
    </tr>
{% endfor %}

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.

users_list = [{'username':'x', 'expense_list':[2,4,5,0,0,0,2], 'sum':13}, ...]

when you do it in the view, and have a list of expenses for each user, calculating the sum should be as easy as

user['sum'] = sum(user['expense_list'])

Then your template code should be simple:

{% for user in users_list %}
    <tr>
    <td>{{ user.username }}</td>
    {% for expense in user.expense_list %}
        <td>{{ expense }}</td>
    {% endfor %}
    <td>{{ user.sum }}</td>
    </tr>
{% endfor %}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文