【问题标题】:Django template - for loop and then if statementDjango 模板 - for 循环然后 if 语句
【发布时间】:2021-06-30 16:54:36
【问题描述】:

我有这个循环:

<div class="product-gallery-preview order-sm-2">
                            {% for images in Artikel.productimages_set.all %}
                            {% if images.Position == 1 %}
                                <div class="product-gallery-preview-item active" id="gallery{{ images.Position }}">
                            {% else %}
                                <div class="product-gallery-preview-item" id="gallery{{ images.Position }}">

我认为问题是所有 div 类都是活动的,因为找到的第一张图片都是如此。 是否可以循环检查所有图片是否位于位置 1 并仅输出我尝试打印的 div 框?

【问题讨论】:

    标签: django django-templates django-template-filters


    【解决方案1】:

    你可以使用forloop.counter...

    {% if forloop.counter == 1 %}
      <div class="product-gallery-preview-item active" id="gallery{{ images.Position }}">
    {% else %}
      ....
    {% endif %}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-25
      • 1970-01-01
      • 1970-01-01
      • 2013-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多