Django 字符串索引超出范围
很困惑。我有一个包含 32 个项目的变量,我正在尝试执行 for 循环,但它显示“渲染时捕获 IndexError:字符串索引超出范围”
有什么想法吗?该变量绝对不为空。
{% 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 %}
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 %}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信问题可能出在值的
photo.images
部分。照片对象中的images
是数组还是集合?如果它是一个数组,则images.low_resolution
正在尝试访问数组中low_resolution
索引值处的图像,这可能不是您想要的(或者可能是是???)。您可能需要添加一些逻辑来循环photo.images
,而不是尝试按照现在的方式访问它。有关其他信息,请参阅此答案:How to access array elements in a Django template?< /a>
I believe the problem might be with the
photo.images
part of the value. Isimages
an array or collection in the photo object? If it is an array, theimages.low_resolution
is trying to access the image in the array at the index value oflow_resolution
which is probably not what you want (or maybe it is???). You might need to add some logic to loop over thephoto.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?