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 %}
4
  • 1
    the error might be getting triggered from somewhere else. Looking to code that resolves for photo.images.low_resolution.url? Commented Apr 26, 2011 at 4: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 Commented Apr 26, 2011 at 4:03
  • 1
    traceback - something is taking an index of something, somewhere :D. Edit: Wow, the power of the Python tag. Commented Apr 26, 2011 at 4: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. Commented Apr 26, 2011 at 4:04

1 Answer 1

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?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.