how to show image with index in python as context variable

112 views
Skip to first unread message

Shamaila Moazzam

unread,
Apr 25, 2017, 7:07:08 AM4/25/17
to Django users
hi,

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 ....?

regards.
shamaila

Dartos

unread,
Apr 26, 2017, 9:54:24 AM4/26/17
to Django users
Hi,

If you're using the django template language, use a dot instead of [].

Try this: {{ item.0.image.url }}

For {{ foo.bar }}, the django template language tries the following:

foo[bar]    # dict lookup
foo
.bar    # property
foo
[int(bar)]    # index lookup
foo
.bar()    # callable

Reference for django 1.11:
https://github.com/django/django/blob/1.11/django/template/base.py#L869

Regards,
Dartos
Reply all
Reply to author
Forward
0 new messages