Wednesday, April 26, 2017

Re: how to show image with index in python as context variable

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

On Tuesday, April 25, 2017 at 7:07:08 AM UTC-4, Shamaila Moazzam wrote:
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

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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/47e2daf0-9d5d-4297-b2ef-42cd9692bf7b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment