【问题标题】:All orders are not showing所有订单均未显示
【发布时间】:2020-11-23 11:34:18
【问题描述】:

我想在我的面板的表格中显示所有订单,但它没有显示。它显示在 django admin 中,但没有显示在我的面板中。

这是我的 Views.py:

class AdminPanel(View):
    def get(self, request):
        products = Product.get_all_products()
        cats = Category.get_categories()
        orders = Order.get_all_orders()
        args = {'products':products, 'cats':cats, 'orders':orders}
        return render(self.request, 'adminpanel/main.html', args)

这是我的 HTML 文件:

<table class="table table-bordered">
  <thead>
    <tr>
      <th scope="col">Sno.</th>
      <th scope="col">Date</th>
      <th scope="col">Invoice ID</th>
      <th scope="col">Customer Name</th>
      <th scope="col">Phone Number</th>
      <th scope="col">Product Quantity</th>
      <th scope="col">Status</th>
      <th scope="col">Total</th>
      
    </tr>
  </thead>
  <tbody>
    {% for order in orders %}
    {% if orders %}
    <tr>
      <th scope="row">{{forloop.counter}}</th>
      <td>{{order.date}}</td>
      <td>GNG#{{order.id}}</td>
      <td>{{order.fname}}</td>
      <td>{{order.phone}}</td>
      <td>{{order.quantity}}</td>
      <td>{{order.status}}</td>
      <td>{{order.price}}</td>
    </tr>
    {% else %}
    <h5 class="badge badge-danger">No Orders Found</h5>
    {%endif%}
    {% endfor %}
  </tbody>
</table>

在我的model.py中我有这个方法:

@staticmethod
    def get_all_orders(self):
        return Order.objects.all()

型号:

class Order(models.Model):
    product = models.ForeignKey(Product, on_delete=models.CASCADE, related_name="product")
    customer = models.ForeignKey(Customer, on_delete=models.CASCADE)
    quantity = models.IntegerField(default=1)
    fname = models.CharField(max_length=100, null=True)
    address = models.CharField(max_length=1000, null=True)
    phone = models.CharField(max_length=12, null=True)
    price = models.IntegerField()
    date = models.DateField(datetime.datetime.today, null=True)
    status = models.ForeignKey(Status, on_delete=models.CASCADE, blank=True, null=True)


    @staticmethod
    def get_all_orders(self):
        return Order.objects.all()

【问题讨论】:

  • orders = Order.get_all_products() 是做什么的?
  • 抱歉应该是 get_all_orders 我更正了,但仍然没有数据@WillemVanOnsem
  • 但是DJango不存在该功能,您自己正常实现了。可以分享一下这个函数的细节吗?
  • 您是否在模型上编写了自定义方法来获取所有对象?如果是这样,请分享代码,因为这些看起来不像标准的 django 查询集代码。另外关于获取订单,您使用了get_all_products
  • 我在订单模型下我有这个方法@staticmethod def get_all_orders(self): return Order.objects.all()

标签: python django


【解决方案1】:

这里我没有把订单变量放在正确的函数中

所以函数看起来像这样 ::

def admin(request):
orders = Order.objects.all()
params = {'orders':orders}
return render(request, 'adminpanel/mani.html', params)

【讨论】:

    【解决方案2】:

    函数get_all_orders 没用。它没有什么特别的。只需在视图中使用:

    orders = Order.objects.all()
    

    【讨论】:

      猜你喜欢
      • 2013-09-15
      • 1970-01-01
      • 2014-07-04
      • 1970-01-01
      • 1970-01-01
      • 2014-12-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多