I'm trying to display multiple images for one product but the images won't display and i don't know why can anyone help.
My Models. I have to image field one is used to display and product and if the user wants more detail the other is used to show more images of the product.
class Product(models.Model):
name = models.TextField()
slug = models.SlugField(null=True, unique=True)
description = models.TextField()
stock = models.SmallIntegerField()
price = models.DecimalField(max_digits=10, decimal_places=2)
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('single',args=[self.id,self.slug])
class ProductImage(models.Model):
product = models.ForeignKey('Product', on_delete=models.CASCADE)
image = models.ImageField(upload_to='images/', blank=True)
featured = models.BooleanField(default=False)
thumbnail = models.BooleanField(default=False)
updated = models.DateTimeField(auto_now_add=False, auto_now=True)
active = models.BooleanField(default=True)
def __unicode__(self):
return self.product.image
Views
def single(request, id, slug):
try:
# products = Product.objects.get(slug=slug)
product = get_object_or_404(Product,
id=id,
slug=slug)
images = ProductImage.objects.filter(product=product)
return render(request, "single.html",
{'product': product},
{'images':images})
except:
raise Http404
How I tried to display the images on the Html page.
{% for img in images %}
{% if item.featured %}
<img class="img-responsive"src="{{ MEDIA_URL }}{{ img.image.url }}" height="px" width="px" class="pr-5 mr-5">
{% endif %}
{% if not item.featured %}
<img class="img-responsive"src="{{ MEDIA_URL }}{{ img.image.url }}" height="px" width="px" class="pr-5 mr-5">
{% endif %}
{% endfor %}
Settings
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'assets'),
)
CRISPY_TEMPLATE_PACK = 'bootstrap3'
CRISPY_TEMPLATE_PACK = 'bootstrap4'
LOGIN_REDIRECT_URL = '/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
LOGIN_URL = '/account/login/'