Reputation: 35
These are my models.py
class Supplier(models.Model):
name = models.CharField(max_length=200, null=True)
phone = models.CharField(max_length=200, null=True, blank=True)
email = models.CharField(max_length=200, null=True, blank=True)
date_created = models.DateTimeField(auto_now_add=True, null=True)
def __str__(self):
return self.name
class Product(models.Model):
sku = models.IntegerField(null=True)
description = models.CharField(max_length=30)
costprice = models.FloatField(null=True, max_length=99, blank=True)
retailprice = models.FloatField(null=True, max_length=99, blank=True)
barcode = models.CharField(null=True, max_length=99, unique=True, blank=True)
image = DefaultStaticImageField(null=True, blank=True,
default='images/item_gC0XXrx.png')
supplier = models.ForeignKey(Supplier, on_delete=models.CASCADE, null=True, blank=True)
def __str__(self):
return self.description
I have a supplierpage/str:pk_supplier/ and inside it I want to display all products that belong to that specific supplier in a table like:
<tr>
<th>ID</th>
<th>Description</th>
<th>Cost</th>
<th>Retail Price</th>
<th>Barcode</th>
</tr>
</thead>
<tbody>
<tr>
**{% for ??? in ???? %} <-----what should i put here??**
<td> <a href="{% url 'productpage' product.id %}">{{product.id}}</a></td>
<td><h6><strong>{{product.description}}</strong></h6></td>
<td>£{{product.costprice |floatformat:2}}</td>
<td>£{{product.retailprice |floatformat:2}}</td>
<td>{{product.barcode}}</td>
</tr>
{% endfor %}
Upvotes: 1
Views: 155
Reputation: 476574
In your view you can filter on the pk_supplier
, so:
def my_view(request, pk_supplier):
products = Product.objects.filter(supplier_id=pk_supplier)
return render(request, 'name-of-template.html', {'products': products})
In the template, you then enumerate over your products
:
{% for product in products %}
<tr>
<td> <a href="{% url 'productpage' product.id %}">{{product.id}}</a></td>
<td><h6><strong>{{product.description}}</strong></h6></td>
<td>£{{product.costprice |floatformat:2}}</td>
<td>£{{product.retailprice |floatformat:2}}</td>
<td>{{product.barcode}}</td>
</tr>
{% endfor %}
Upvotes: 1