'Order' 对象不可迭代 [Django]

'Order' object is not iterable [Django]

一开始我会注意到我是初学者 :P 我的 django models.py 中有两个模型,我希望它们显示在页面上。问题是我收到关于没有可能的迭代的错误,我不知道为什么。

此外,我正在学习 Youtube 的速成课程,并更改了一些东西供我使用:)

我在 google 上没有找到任何有用的提示,请您指点一下好吗?

谢谢!

models.py

from django.db import models

# Create your models here.
class Supplier(models.Model):
    name = models.CharField(max_length=200, null=True)
    phone = models.CharField(max_length=200, null=True)
    email = models.CharField(max_length=200, null=True)
    date_created = models.DateTimeField(auto_now_add=True)
    def __str__(self):
        return self.name

class Order(models.Model):
    STATUS = (
            ('Pending Order', 'Pending Order'),
            ('Pending PR', 'Pending PR'),
            ('Declined by SME', 'Declined by SME'),
            ('Declined by Finance', 'Declined by Finance'),
            ('Ordered', 'Ordered'),
            ('Canceled', 'Canceled'),
            ('Delivered', 'Delivered'),
            )
    product = models.CharField(max_length=200, null=True)
    link = models.CharField(max_length=400, null=True)
    supplier = models.ForeignKey(Supplier, null=True, on_delete=models.SET_NULL)
    date_created = models.DateTimeField(auto_now_add=True, null=True)
    status = models.CharField(max_length=200, null=True, default='Pending Order', choices=STATUS)
    amount = models.CharField(max_length=40, null=True)
    comment = models.CharField(max_length=400, null=True, blank=True)
    requester = models.CharField(max_length=40, null=True)

    def __str__(self):
        return self.product

views.py

from django.shortcuts import render
from .models import *

# Create your views here.

def home(request):
    return render(request, 'accounts/dashboard.html')

def products(request):
    return render(request, 'accounts/products.html')

def customer(request):
    return render(request, 'accounts/customer.html')

def orders(request):
    orders = Order.objects.all()
    customers = Supplier.objects.all()
    context = {'orders': orders, 'customers': customers}
    return render(request, 'accounts/orders.html', {'orders': Order})

我的 HTML 的一部分只有 bootstrap 和 HTML

    <table class="table table-sm">
                <tr>
          <th>Supplier</th>
          <th>Product (Name)</th>
                    <th>Product (ID)</th>
                    <th>Date Orderd</th>
                    <th>Status</th>
          <th>Requester</th>
                    <th>Update</th>
                    <th>Remove</th>
                </tr>
          {% for order in orders %}
            <tr>
              <td>{{order.supplier}}</td>
              <td>{{order.product}}</td>
              <td>{{order.link}}</td>
              <td>{{order.date_created}}</td>
              <td>{{order.status}}</td>
              <td>{{order.requester}}</td>
          {% endfor %}
            </table>
def orders(request):
    orders = Order.objects.all()
    customers = Supplier.objects.all()
    context = {'orders': orders, 'customers': customers}
    return render(request, 'accounts/orders.html', context)

试试这个。 在渲染中,你传递的是 Order class 而不是你的查询集 orders