S
S
StasShk2016-03-15 05:23:31
Django
StasShk, 2016-03-15 05:23:31

How to display a card in a django template?

Good afternoon! I can't figure out how to display the image. My product images are stored in a separate model (so that you can upload several):

class Product(models.Model):
    ....

class ProductImage(models.Model):
    property = models.ForeignKey(Product, related_name='images', null=True)
    image = models.ImageField(verbose_name='Image', upload_to=get_image_path)

How to display one random image for a list of products in a template?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2016-03-15
@StasShk

models.py

class Product(models.Model):
    @property
    def random_pic(self):
        return self.images.order_by('?').first()

template.html
<img src="{{ product.random_pic.image.url }}">

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question