Displaying the multiple images in the product detail page

1,605 views
Skip to first unread message

hadiqak...@gmail.com

unread,
Apr 25, 2017, 7:40:46 AM4/25/17
to Django users
I have created a separate image class where you can add images but I'm unable to show the multiple images on the product detail page. If anyone can guide me, it would be great. 

class Image(models.Model):
active = models.BooleanField(default=True)
product = models.ForeignKey(Product)
image = models.FileField(null=True)
title = models.CharField(max_length=120)

def __unicode__(self):
return self.title

Matthew Pava

unread,
Apr 25, 2017, 9:05:02 AM4/25/17
to django...@googlegroups.com

You could reference the images from your Product object like so:

product.image_set.all()

You may want to use models.ImageField, which inherits from models.FileField, instead of models.FileField

 

For more information:

https://docs.djangoproject.com/en/1.11/topics/db/examples/many_to_one/

https://docs.djangoproject.com/en/1.11/ref/models/fields/#django.db.models.FileField

 

 

In your template, you would probably want to use a for loop similar to this:

{% for i in product.image_set.all %}
   <img src=”{{ i.name }}” /><br />
{% endfor %}

--
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...@googlegroups.com.
To post to this group, send email to django...@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/fcff49ff-f999-41eb-a5bb-2abc567c2f73%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

hadiqak...@gmail.com

unread,
Apr 25, 2017, 12:53:15 PM4/25/17
to Django users, Matthe...@iss.com
i am trying to display a set of images with the use of index.
here is the code:
     <div>
         {% for item in product.productimage_set.all %}
        
            <img src="{{ item[0].image.url }}" style="height:100px;widht:50px"/>
      
       
         {% endfor %}

     </div>

the above code is displaying a set of images for one product .right.
i want to display it like {{ item[0].image.url }}
how can i do that ....?

To post to this group, send email to djang...@googlegroups.com.

Shamaila Moazzam

unread,
Apr 25, 2017, 2:55:47 PM4/25/17
to Django users, Matthe...@iss.com
   <div>
         {% for item in product.productimage_set.all %}
        
            <img src="{{ item[0].image.url }}" style="height:100px;widht:50px"/>
      
       
         {% endfor %}

     </div>

this code is displaying different images of a single product ,but the question is how to slice them ,how to get image[0], image[1] ......on the product page...

can any one help??

m712 - Developer

unread,
Apr 25, 2017, 11:29:56 PM4/25/17
to Shamaila Moazzam, Matthe...@iss.com, Django users

Stop replying over and over again with the same content, we saw it.
Regarding your question, I don't know whether you're referring to item[0].image[x] or item[x]. If you're refferring to the first one then please consider renaming your variables. In any case; this:

<div>

         {% for item in product.productimage_set.all %}

            {% for im in item[0].image %}

                <img src="{{ im.url }}" style="height:100px;width:50px"/>

         {% endfor %}
</div>
should work for the former, and this:

<div>

         {% for item in product.productimage_set.all %}

            {% for i in item %}

                <img src="{{ i.image.url }}" style="height:100px;width:50px"/>

         {% endfor %}
</div>
should work for the latter. If you need to loop both item and image you should be able to do it by now.

To post to this group, send email to django...@googlegroups.com.

m712 - Developer

unread,
Apr 25, 2017, 11:34:51 PM4/25/17
to Shamaila Moazzam, Django users, Matthe...@iss.com

I forgot a second {% endfor %} below the <img> there, but you get the idea.

Reply all
Reply to author
Forward
0 new messages