python - 我可以使用 Django 中的中断模板标签吗
问题描述
我正在运行 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 %}
有谁知道这个的另一种解决方案?
解决方案
通过简单地添加|slice:":1"
到 for 循环的末尾,我达到了我想要的结果。一旦它从第一篇文章中抓取了第一张图片,它就停止了我的 for 循环。
{% for article in object_list|slice:":1" %}
推荐阅读
- java - BinaryTree() 在 BinaryTree 类中做了什么?
- c# - 我们可以使用 Private Constructor 创建超过 1 个实例吗?
- android - 如何访问工作管理器中的房间数据库以定期同步?
- calendar - Pikaday - 不同颜色的不同活动
- c# - 生成的 C 代码中 fixdt 的表示
- javascript - 如何将https请求中的会话或cookie设置为url?
- toolbar - 如何从 UITableView 小键盘工具栏中添加的向上、向下箭头遍历单元格?
- python-3.x - 如何解决具有 64 位 Windows 和 8GB RAM 的 Python 3.6 中的内存错误?
- html - 从下拉列表中选择给定值并单击添加按钮
- c# - 为什么我不能重命名文件?