开发者

How to aggreate in html django

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


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 %}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜