I am trying to build a website where you can sell stuff. I want the user to be able to upload multiple images per product/post. I think I have figured that part out. But I am struggling to display one image per product. Here are the details:
models.py (I have left out some basic methods)
class Product(models.Model):
title=models.CharField(max_length=200)
description=models.TextField(max_length=1000)
price = models.DecimalField(max_digits=12, decimal_places=2)
user=models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.SET_NULL)
class ProductImages(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL)
product_fk = models.ForeignKey(Product, on_delete=None, to_field="id")
image = models.ImageField(upload_to=image_location)
####################################
views.py
*function for product_detail_view
def products_detail(request, pk):
productt = get_object_or_404(Product, id=pk)
images = get_list_or_404(ProductImages, product_fk=pk)
context = {'images': images, 'productt':productt}
return render(request, 'index5.html', context)
##################################
urls.py
url(r'^list/(?P<pk>\d+)$', views.products_detail, name='list_products_detail'),
##################################
** I need a function to list all products and one image per product. Same as when you search for a product in any website where there is an image per product.
I have tried to solve this on my own, but I was not able.
I have tried using generic.ListView but failed.