I want to get image form Image model for my Product model and show it cart template
This is my Code:
ecommerce/models.py
class Product(models.Model):
name = models.CharField(max_length=200)
content = models.TextField()
excerpt = models.TextField()
price = models.DecimalField(max_digits=6, decimal_places=2)
status = models.IntegerField(default=0)
date = models.DateTimeField(auto_now_add=True)
quantity = models.PositiveIntegerField()
author = models.PositiveIntegerField()
featured_image = models.CharField(max_length=300)
available = models.BooleanField(default=True)
def __str__(self):
return self.name
class Image(models.Model):
product = models.ForeignKey(Product, on_delete=models.CASCADE)
image = models.ImageField()
cart/views.py
@require_POST
def cart_add(request, product_id):
cart = Cart(request)
product = get_object_or_404(Product, pk=product_id)
form = CartAddProductForm(request.POST)
if form.is_valid():
cd = form.cleaned_data
cart.add(product=product,
quantity=cd['quantity'],
update_quantity=cd['update'])
return redirect('cart:cart_detail')
def cart_detail(request):
cart = Cart(request)
for item in cart:
item['update_quantity_form'] = CartAddProductForm(initial={'quantity': item['quantity'],
'update': True})
return render(request, 'cart_detail.html', {'cart': cart})
cart/templates/cart.html
<tbody>
{% for item in cart %}
{% with product=item.product %}
<tr>
<td>
{% for product in products %}
<img src="{{ product.first_image.src }}" alt="" width="auto" height="340"/>
{% endfor %}
</td>
<td>{{ product.name }}</td>
.
.
.
</tr>
{% endwith %}
{% endfor %}
</tbody>
i have read this link
but it is no use to me, please help me, I am newby to this
Thank you