如何在单独的行上显示列表中的数据

时间:2012-08-28 22:25:17

标签: python django

我正在建立库存系统,我有一个页面,可以按类别查看销售历史记录,它将显示所有已售商品,销售数量,库存总量,剩余数量,销售额,利润等。 ..无论何时我按类别搜索(例如文具),显示如下所示。

Item Name    stock qty    sold qty     total stock   total amount            profit
Books          990        45 75 60     1035 900 990   14567 15666 12666    1777 1555 17777
Sciences       825        45 75 60     1035 900 990    14567 15666 12666    1777 1555 17777
Comics         930        45 75 60     1035 900 990    14567 15666 12666    1777 1555 17777

但我真的想要的是:

Item Name    stock qty    sold qty     total stock   total amount            profit
Books          990        45            1035          14567                  1777 
Sciences       825        75            1035          15666                  1555 
Comics         930        60            990           12666                  17777

项目名称和库存数量已从数据库中调用并附加到列表中。对于销售的商品数量,我从数据库中调用此类商品的每个实例并计算其总数,然后附加到列表中。所以我想把他们各自的项目显示出来,同样适用于金额和专业。我只想要的是如何操纵模板以便适当地显示它。我的观点代码如下:

def search_category (request):
    collate=[]
    gains=[]
    amount=[]
    name=[]
    store=[]
    qty=[]
    if 'q' in request.GET and request.GET['q']:
        q = request.GET.get('q','')
        items = Item.objects.filter(category__name__exact=q)
        for item in items:
            collate.append(item.subcategory.subcategoryname)
            qty.append(int(item.quantity))
        len_collate=len(collate)
        count=0
        #for pick in collate:
        keep1=[]
        amt_save=[]
        gain_save=[]
        t=[]
        all_amt_save=[]
        #sum=0
        #t_amount=0
        #t_gain=0
        while count < len_collate:
            soldqty=[]

            #histories = RecordSales.objects.filter(ItemName__subcategoryname__exact=pick)#here

            histories = RecordSales.objects.filter(ItemName__subcategoryname__exact=collate[count])
            lenght=len(histories)
            for history in histories:


                soldqty.append(history.quantity)

                #keep1=[] #array to store sum
                len_qty=len(soldqty)
                #count=0
                #while count < lenght:
                sum=0
                sums=[]
                t_stock=[]
                #for num in soldqty:
                count_qty=0
                #for num in soldqty:
                while count_qty < len_qty:
                    sum=sum + soldqty[count_qty]
                    #t_stock=qty[count_qty] + sum
                    count_qty=count_qty + 1
                amount.append(history.total)
                t_amount=0

                amt_count=0
                while amt_count < len(amount):
                    t_amount=t_amount + amount[amt_count]
                    amt_count=amt_count + 1
                gains.append(history.profit)
                t_gain=0
                gain_count=0
                while gain_count < len(gains):
                    t_gain=t_gain + gains[gain_count]
                    gain_count=gain_count + 1
                name.append(history.ItemName)



            #keep1.append(sum)
            len_keep1=len(keep1)
            amt_save.append(t_amount)
            gain_save.append(t_gain)
            count=count + 1
            keep1.append(sum)
            all_amt_count=0
            all_amt_total=0
            while all_amt_count < len(amt_save):
                all_amt_total= all_amt_total + amt_save[all_amt_count]

                #all_amt_save.append(all_amt_total)
                all_amt_count=all_amt_count + 1
            all_g_count=0
            all_g_total=0
            while all_g_count < len(gain_save):
                all_g_total=all_g_total + gain_save[all_g_count]
                all_g_count=all_g_count + 1
            t_count=0
            while t_count < len(keep1):
                t_stock=keep1[t_count] + qty[t_count]

                t_count=t_count + 1
            store.append(t_stock)
            total_amount=0
            keep_total=[]
            for total in amount:
                total_amount=total_amount + total
                keep_total.append(total_amount)
                    #total_amount=0


        return render_to_response('sell_category.html',
        {'items':items,'query': q,'keep1':keep1,'name':name,'items':items,
        'keep_total':keep_total,'t':store,'amt_save':amt_save,'gain_save':gain_save,'all_amt':all_amt_total,'t_gain':all_g_total,'len':len_keep1,

        })
    else:
        return render_to_response('sell_category.html', {'error': True})

感谢。

1 个答案:

答案 0 :(得分:1)

当您将所有内容附加到列表中时,您可以使用zip()和一些字符串格式来获得所需的结果:

items=['Books','Sciences','Comics']
stock_qua=[990,825,930]
sold_qua=[45,75,60]
total_sto=[1035,1035,990]
total_amt=[14576,15666,12666]
profit=[1777,1555,1777]

print "{0:12s}{1:12s}{2:12s}{3:15s}{4:15s}{5:12s}".format("Item Name", "stock qty","sold qty","total stock","total amount","profit")

for item,stock,sold,tot_st,amt,pro in zip(items,stock_qua,sold_qua,total_sto,total_amt,profit):
    print "{0:12s}{1:^10d}{2:^10d}{3:^15d}{4:^15d}{5:^12d}".format(item,stock,sold,tot_st,amt,pro)

<强>输出:

Item Name   stock qty   sold qty    total stock    total amount   profit      
Books          990        45         1035           14576         1777    
Sciences       825        75         1035           15666         1555    
Comics         930        60          990           12666         1777