When I am filtering the product but when I run the project I don't get the images
If I change the code Product.objects.filter(slug=slug) To replce the code with Product.objects.get(slug=slug) the I am facing this type off error in my trminal
Django 'Product' object is not iterable
Views.py
def product_detail(request, slug):
try:
product = Product.objects.filter(slug=slug)
context = {
'product': product,
}
return render(request, 'buyer/product_details.html', context)
except Product.DoesNotExist:
return render(request, 'buyer/product_details.html', {'error': 'No data found.'})
URLs.py
path('product/<slug:slug>', views.product_detail, name="product_detail"),
Models.py
class Product(models.Model):
total_quantity = models.IntegerField()
availability = models.IntegerField()
feature_image = models.ImageField(upload_to='media/Product_image')
product_name = models.CharField(max_length=100)
price = models.IntegerField()
discount = models.IntegerField()
product_information = RichTextField()
model_name = models.CharField(max_length=100)
categories = models.ForeignKey(MainCategory, on_delete=models.CASCADE)
tags = models.CharField(max_length=100)
description = RichTextField()
section = models.ForeignKey(Section, on_delete=models.DO_NOTHING)
slug = models.SlugField(default='', max_length=500, null=True, blank=True)
def __str__(self):
return self.product_name
def get_absolute_url(self):
from django.urls import reverse
return reverse("product_detail", kwargs={'slug': self.slug})
class Meta:
db_table = "buyer_Product"
def create_slug(instance, new_slug=None):
slug = slugify(instance.product_name)
if new_slug is not None:
slug = new_slug
qs = Product.objects.filter(slug=slug).order_by('-id')
exists = qs.exists()
if exists:
new_slug = "%s-%s" % (slug, qs.first().id)
return create_slug(instance, new_slug=new_slug)
return slug
def pre_save_post_receiver(sender, instance, *args, **kwargs):
if not instance.slug:
instance.slug = create_slug(instance)
pre_save.connect(pre_save_post_receiver, Product)
class ProductImage(models.Model):
product = models.ForeignKey(Product, on_delete=models.CASCADE)
image_url = models.ImageField(upload_to='media/Product_image')
def __str__(self):
return self.product.product_name
HTML Page
<div >
{% for i in product.productimage_set.all %}
<figure >
<img src="{{ i.image_url }}" alt="product image"/>
</figure>
{% endfor %}
</div>
<!-- THUMBNAILS -->
<div >
{% for i in product.productimage_set.all %}
<div><img src="{{ i.image_url }}" alt="product image"/></div>
{% endfor %}
</div>
</div>
<!-- End Gallery -->
CodePudding user response:
You need to pass .url
{% for i in product.productimage_set.all %}
<figure >
<img src="{{ i.image.url }}" alt="product image"/>
</figure>
{% endfor %}
CodePudding user response:
that's because:
Product.objects.get(slug=slug) returns a single object type porduct. and one single object is not iterable
and
Product.objects.filter(slug=slug) returns a Queryset[] of products (list of products)
so if you are looking to treat multiple products use the second way, but if you are looking to treat one single product, use the first one