简体   繁体   中英

Is there a break template tag in Django I can use

I am running a for loop to find an image, but I want to block the for loop after it has found an image.

I have found this website that seems to solve my problem but doesn't seem to be updated 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 %}

Does anyone know another solution for this?

By simply adding |slice:":1" to the end of the for loop I achieved the result I wanted. This stopped my for loop as soon as it grabbed the first image from the first article.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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