Skip to main content
 首页 » 编程设计

python之如何忽略 FOR 函数中列表中的第一项

2025年05月04日14bluestorm

我有以下 for 循环,可以输出列表中的所有照片:

{% if photos %} 
{% for photo in photos %} 
    {% thumbnail photo.photo "100x100" crop="center" as im %} 
    <img src="{{ im.url }}" alt="User's photos" data-ajax="{% url 'photo_increase_view' pk=photo.id %}"/> 
    {% endthumbnail %} 
{% endfor %} 
{% endif %} 

如何编辑它以忽略列表中的第一个结果(即显示项目 2、3、4...等)

请您参考如下方法:

使用slice

替换

{% for photo in photos %} 

通过

{% for photo in photos|slice:"1:" %} 

那么,完整的代码

{% if photos %} 
{% for photo in photos|slice:"1:" %} 
    {% thumbnail photo.photo "100x100" crop="center" as im %} 
    <img src="{{ im.url }}" alt="User's photos" data-ajax="{% url 'photo_increase_view' pk=photo.id %}"/> 
    {% endthumbnail %} 
{% endfor %} 
{% endif %}