繁体   English   中英

我可以使用Django中的break模板标签吗

[英]Is there a break template tag in Django I can use

我正在运行for循环来查找图像,但是我想在找到图像后阻止for循环。

我发现此网站似乎可以解决我的问题,但似乎未针对Django 1.10.5更新: https ://djangosnippets.org/snippets/2093/。

{% for article in object_list %}
    {% if article.get == true %}
        <div id="tagHeader" style="background: url({{ article.get_thumbnails|varkey:'grid_thumb' }}) no-repeat center center fixed;"><!-- background image not working becuase home_background isn't in CMS -->
    {% else %}
        <div id="tagHeader" style="background: url({% static 'publicnfx/images/baconburger.jpg' %}) no-repeat center center fixed;">
    {% endif %}
{% endfor %}

有人知道其他解决方案吗?

通过简单地在for循环的末尾添加|slice:":1" ,我获得了想要的结果。 一旦从第一篇文章中获取了第一张图片,就停止了我的for循环。

{% for article in object_list|slice:":1" %}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM