VATSAL JAIN
VATSAL JAIN

Reputation: 581

Order object not iterable in python error in django

This is my models.py:

class Order(models.Model):
    customer=models.ForeignKey(Customer,on_delete=models.SET_NULL,null=True,blank=True)
    date_ordered=models.DateTimeField(auto_now_add=True)
    complete=models.BooleanField(default=False,null=True,blank=False)
    transaction_id=models.CharField(max_length=100,null=True)

This is my views.py:

def lobby(request):
    customer=request.user.customer
    order, created=Order.objects.get_or_create(customer=customer, complete=False)
    shippingddress=ShippingAddress.objects.filter(customer=customer)
    
    context={
        'order':order,
        'ship':shippingddress
    }

    return render(request,"lobby.html",context)

and this is my html:

    {% for ord in order %}
              <div class="single_confirmation_details">
                <h4>Order Info</h4>
                <ul>
                  <li>
                    <p>Order Number</p><span>: {{ord.id}}</span>
                  </li>
</ul>
</div>
{% endfor %}

I am getting this error:'Order' object is not iterable I dont know why this error is coming. I have passed shippingaddress in context which is working fine but for Order it is showing error. Please help me with this.

Upvotes: 2

Views: 264

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477170

order is a single order, so it makes no sense to iterate over it.

In the template you thus should remove the {% for … %} … {% endfor %} loop:

<div class="single_confirmation_details">
    <h4>Order Info</h4>
    <ul>
        <li><p>Order Number</p><span>: {{ order.pk }}</span></li>
    </ul>
</div>

Upvotes: 3

Related Questions