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. -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/6f8e80dc-e410-4eec-90ab-b9352010cff6%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.