1

Very confused. I have a variable that has 32 items in it, and I'm trying to do a for loop but it's saying "Caught IndexError while rendering: string index out of range"

Any ideas? The variable definitely isn't empty.

{% if photos %}
    <ul class="photo-grid">
        {% for photo in photos %}
                <li>
                        <img src="{{ photo.images.low_resolution.url }}" />
                </li>
        {% endfor %}
    </ul>
{% else %}
    No photos found.
{% endif %}
Brenden
  • 8,264
  • 14
  • 48
  • 78
  • 1
    the error might be getting triggered from somewhere else. Looking to code that resolves for `photo.images.low_resolution.url`? – Senthil Kumaran Apr 26 '11 at 04:03
  • 1
    are u sure its not from your views.py that your error arises? From what i know, by default variable error in the templates will fail silently – goh Apr 26 '11 at 04:03
  • 1
    traceback - something is taking an index of something, somewhere :D. Edit: Wow, the power of the Python tag. – Yuji 'Tomita' Tomita Apr 26 '11 at 04:03
  • 1
    Looks actually like one of your `photo` dict/object doesn't have the keys you are trying to access. My guess is that either `photo.images` or `photo.images.low_resolution` or `photo.images.low_resolution.url` is missing. – Torsten Engelbrecht Apr 26 '11 at 04:04

1 Answers1

3

I believe the problem might be with the photo.images part of the value. Is images an array or collection in the photo object? If it is an array, the images.low_resolution is trying to access the image in the array at the index value of low_resolution which is probably not what you want (or maybe it is???). You might need to add some logic to loop over the photo.images rather than trying to access it the way you are now.

See this answer for other info: How to access array elements in a Django template?

Community
  • 1
  • 1
Andy White
  • 86,444
  • 48
  • 176
  • 211