[英]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.