2011-03-07 72 views
0

寫了Excel格式的HTML標準來表示日期明智計數如何在HTML的Django aggreate

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 
  • 在這裏,我想在 年底彙總的數據呢?

  • 有沒有什麼最好的方法可以做到這一點?

在此先感謝

+4

唉,不要在您的視圖中手動創建HTML。有一個完整的模板語言。 – 2011-03-07 18:24:47

+0

@ daniel,我嘗試了很多方法,但最後我手動創建了HTML,你能否給我建議一些代碼示例? – sush 2011-03-08 01:24:45

回答

0

從視圖中刪除所有的HTML創建代碼。

視圖需要爲模板創建所需的變量,模板應該處理格式。也許創建一個用戶列表,併爲每個用戶列出長度爲calendar.mdays [month]的列表,其中包含此用戶的廣度和總和變量。

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

謝謝大家的回答,但我在這裏有一個疑問,我的輸出是這種格式,users_list = [{'username':'x','expense_list':1,'date':2011-03-02}, {'username':'x','expense_list':2,'date':2011-03-04},...],如何將其設置爲users_list = [{'username':'x','expense_list ':[2,4,5,0,0,0,2],'sum':13},...] – sush 2011-03-09 07:29:14

+0

更改您的get_detail以返回給定日期範圍內用戶費用的列表。 – 2011-03-09 21:18:11