如何在Django模板中将熊猫数据框显示为表格

时间:2019-04-08 12:15:55

标签: django-templates

我有一个Pandas表,该表中填充了值。该视图将该数据发送到我的模板。不幸的是,尽管我可以在python shell中循环它的值。我附上桌子并尝试:

我的表格:(作为输出)在视图中,但要通过模板在其中显示

     NoofProduct  SumOfQuantity  mine_id  prod_category      nearbyExpiry
0            1              0        1              1                 1
1            2              8        2              2                 2
2            1              4        3              3  no nearby expiry

In views, .py-I have this function

def CategoryWiseViewAll(request, template_name='CategoryViewAll.html'):
    end_date = today + datetime.timedelta(days=61))
    book1=pd.DataFrame(ProductModel.objects.values('prod_category').annotate(SumOfQuantity=Sum('quantity')).annotate(NoofProduct=Count('prod_name')).values('prod_category', 'SumOfQuantity','NoofProduct','mine_id'))
   book2=pd.DataFrame(ProductModel.objects.values('prod_category').filter(prod_expiry__range=(today,end_date)).annotate(SumOfQuantity=Sum('quantity')).annotate(nearbyExpiry=Count('prod_name')).values('prod_category','nearbyExpiry'))
    bookqs1 = ProductModel.objects.values_list('prod_category').annotate(SumOfQuantity=Sum('quantity')).annotate(NoofProduct=Count('prod_name')).values('prod_category', 'SumOfQuantity','NoofProduct','mine_id')
    datalist1 = []
    datalist2 = []
    data1 = {}
    data2 = {}
    i=0
    if len(book2) == 0:
        datalist1=bookqs1
    else:

        datalist2 = pd.merge(book1, book2, how="left")
        datalist2 = datalist2.fillna("no nearby expiry")
    print (datalist1)
    print (datalist2)
    data1['object_list']=datalist1
    data2['object_list']=datalist2
    return render(request, template_name, data1, data2)


My attempt: As template
<table class="table " id="myTable">
         <thead>
             <tr>
              <th>Sl No</th>
               <th>Mine Name</th>
               <th>Category Name</th>
                <th>No of Product</th>
                 <th>In Stock</th>
                  <th>No. of Prod Expiring Soon...</th>
              </tr>
          </thead>
               <tbody>
                    {% if data1 %} 
                                <!-- {% for book.1 in object_list %} -->
                                {% for i, b in object_list.itertuples %}
                                   <tr>
                                        <td>{{forloop.counter}}</td>
                                        <td>{{ b.mine_id}}</td>
                                        <td>{{ b.prod_category }}</td>
                                        <td>{{ b.NoofProduct }}</td>
                                        <td>{{ b.SumOfQuantity}}</td>
                                        <td> {{ b.nearbyExpiry}} </td>
                                    </tr>   
                                {% endfor %}
                    {% else %}
                               {% for book in object_list %}        
                                        <tr>
                                        <td>{{forloop.counter}}</td>
                                        <td>{{ book.mine_id}}</td>
                                        <td>{{ book.prod_category }}</td>
                                        <td>{{ book.NoofProduct }}</td>
                                        <td>{{ book.SumOfQuantity }}</td>
                                        <td> 0 </td>
                                        </tr>
                                {% endfor %}
                    {% endif %}            
                    </tbody>

 I am receiving below error : 
"Invalid block tag on line 56: 'else', expected 'empty' or 'end'. Did you forget to register or load this tag?"

And not able to show table data in my template or in UI

在python shell(我在其中进行测试)中,我可以通过以下方法循环表。如何在Django模板中循环熊猫表?预先谢谢你。

0 个答案:

没有答案