0

I have gathered data from two tables that is Orders and Purchases (joined) and saved that in dictionary, when I'm printing the qty on the console its giving me the value but when I'm trying to print the values on template it not showing up however columns from the orders table like first name last name status address is shown on the template but qty and product from the purchases table is not showing up on the template but can be easily printed on console can somebody help??? Models.py

    Dispatcher = models.ForeignKey(Dispatchers , null=True, on_delete=models.SET_NULL )
   
    Firstname = models.CharField(max_length=200 , null=True)
    Lastname = models.CharField(max_length=200 , null=True)
    Email = models.CharField(max_length=200 , null=True)
    Address = models.CharField(max_length=200 , null=True)
    Country = models.CharField(max_length=200 , null=True)
    State = models.CharField(max_length=200 , null=True)
    status = models.CharField(max_length=200 , null=True)
    def __str__(self):
     return self.Lastname

class purchases(models.Model):
    orders_id = models.ForeignKey(Orders , null=True, on_delete=models.SET_NULL )
    product =  models.CharField(max_length=200 , null=True)
    qty =  models.CharField(max_length=200 , null=True)
    price = models.CharField(max_length=200 , null=True)
    
    def __str__(self):
     return self.product

views.py file

    dispatcherOrders = {}
    orders = request.user.dispatchers.orders_set.all()
    
    for order in orders:
        dispatcherOrders[order] = purchases.objects.get(orders_id = order.id)
        print(dispatcherOrders[order].qty)
    
    return render(request , 'dispatcherOrders.html',{'dispatcherOrders' : dispatcherOrders})

dispatcherOrders.html

        {%for Orders in dispatcherOrders%}
      <tr>
        <th scope="row">1</th>
        <td>{{Orders.Firstname}}</td>
        <td>{{Orders.Lastname}}</td>
        <td>{{Orders.Address}}</td>
        <td>{{Orders.product}}</td>
        <td>{{Orders.qty}}</td>
        <td>{{Orders.status}}</td>
      </tr>
      {% endfor %}
Syeda
  • 23
  • 4
  • At the end of the for loop try to print `dispatcherOrders` and include the data. It will be helpful to debug the issue – Sabil Aug 29 '21 at 11:46
  • @Sabil this is the result of print(dispatchOrders) {: , : } and print print(dispatcherOrders[order].qty) is 3 – Syeda Aug 29 '21 at 11:58

1 Answers1

0

In your code, dispatcherOrders dictionary keys are orders and dictionary values are purchases:

dispatcherOrders[order] = purchases.objects.get(orders_id = order.id)

yet you are iterating only on dictionary keys, the orders:

{%for Orders in dispatcherOrders%}

To get the info from purchases, iterate the dictionary items too.

So your template code should be similar to

        {%for Orders, Purchase in dispatcherOrders.items %}
      <tr>
        <th scope="row">1</th>
        <td>{{Orders.Firstname}}</td>
        <td>{{Orders.Lastname}}</td>
        <td>{{Orders.Address}}</td>
        <td>{{Purchase.product}}</td>
        <td>{{Purchase.qty}}</td>
        <td>{{Purchase.status}}</td>
      </tr>
      {% endfor %}

Here's how .items work: https://stackoverflow.com/a/6285769/3694363

Pavel Vergeev
  • 3,060
  • 1
  • 31
  • 39