Python 所有的订单都没有显示出来
我想在我的面板的一个表中显示所有订单,但它不显示。它显示在django admin中,但不显示在我的面板中 以下是我的视图.py:Python 所有的订单都没有显示出来,python,django,Python,Django,我想在我的面板的一个表中显示所有订单,但它不显示。它显示在django admin中,但不显示在我的面板中 以下是我的视图.py: class AdminPanel(View): def get(self, request): products = Product.get_all_products() cats = Category.get_categories() orders = Order.get_all_orders()
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>
型号:
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变量放在正确的函数中 因此,函数将如下所示:
def admin(request):
orders = Order.objects.all()
params = {'orders':orders}
return render(request, 'adminpanel/mani.html', params)
功能
获取所有订单
无效。没什么特别的。仅在视图中使用:
orders = Order.objects.all()
什么是订单。get_all_products()做什么?对不起,应该是get_all_orders我更正了,但仍然没有数据@WillemVanonSem但DJango不存在该函数,你自己正常实现了它。你能分享这个函数的细节吗?你有没有在模型上写自定义方法来获取所有对象?如果是这样,请共享代码,因为这些代码看起来不像标准的django queryset代码。另外,关于获取订单,您使用了我在订单模型下使用的
get_all_products
方法@staticmethod def get_all_orders(self):return order.objects.all()
orders = Order.objects.all()