仅在django if

时间:2017-03-18 10:04:57

标签: html django django-templates django-views

我在django视图中有一个car对象如下:

'damages': [
        {
            "location": "Voorbumper",
            "type": "Kras(10 cm, leger)",
            "severity": "Light damage",
            'comment': "This is some comment.",
            "images": [
                'http://pathtophoto.jpg',
                'http://pathtophoto.jpg'
            ]
        },
        {
            "location": "Vleugel rechts voor",
            "type": "Deuk (Licht)",
            "severity": "",
            'comment': "",
            "images": [
                'http://pathtophoto.jpg',
                'http://pathtophoto.jpg',
                'http://pathtophoto.jpg'
            ]
        },
        {
            "location": "Deur links voor",
            "type": "Kras (5 cm, leger)",
            "severity": "",
            'comment': "",
            "images": [
                'http://pathtophoto.jpg'
            ]
        },
        {
            "location": "Waterlijst",
            "type": "Beschadigd",
            "severity": "",
            'comment': "",
            "images": []
        },
        {
            "location": "Antenne",
            "type": "Ontbreekt",
            "severity": "",
            'comment': "",
            "images": [
                'http://pathtophoto.jpg'
            ]
        },
        {
            "location": "Antenne",
            "type": "Ontbreekt",
            "severity": "",
            'comment': "",
            "images": []
        }
 ]

我想循环浏览该对象并显示损坏的图像。但是我想首先用图像显示损坏,以及没有带有适当标题的图像的损坏。

在django模板中,我尝试如下:

{% for damage in car.damages %}

    {% if damage.images|length > 0 %}

        <div class="width-100 pad text-left primary-bg">{% trans "Shades met foto's" %}</div>

        <div class="width-100 mar-top clear-float">
            <div class="width-30 bord-my-way pad-half damage-info">
                <div class="mar-btm-half"><b>{{ damage.location }}</b></div>
                <div class="mar-btm-half">{{ damage.type }}</div>
                <div class="mar-btm-half">{{ damage.severity }}</div>
                <div class="mar-btm-half">{{ damage.comment }}</div>
            </div>
            <div class="width-70 pad-lft damage-photos">
                {% for image in damage.images|slice:"3" %}
                    <div class="width-33 pad-rgh">
                        <img src="{{ image }}" class="width-100"/>
                    </div>
                {% endfor %}
            </div>
            <div class="clear-float"></div>
            {% if forloop.counter == 7 or forloop.counter == 14 or forloop.counter == 21 %}
                <p style="page-break-before: always"></p> {% endif %}

        </div>
    {% endif %}
{% endfor %}

{% for damage in car.damages %}

    {% if damage.images|length == 0 %}
        <div class="width-100 pad text-left primary-bg mar-top">{% trans "Shades zonder foto's" %}</div>
        <div class="green-bg">{{ damage.location }}</div>
    {% endif %}
{% endfor %}

因此,我首先遍历损坏damage.images|length > 0,然后在第二个循环中检查是否没有damage.images|length == 0的图像。但是每个for loop次迭代都会显示标题。

我可以为for loop添加标题。类似的东西:

<div> Damages with photos </div>
{% for damage in car.damages %}
   {% if damage.images|length > 0 %}
      // Show it
   {% endif %}
{% endfor %

并且

<div> Damages without photos </div>
{% for damage in car.damages %}
   {% if damage.images|length == 0 %}
      // Show it
   {% endif %}
{% endfor %

但有时我只有图像损坏或只有没有图像的损坏,然后我看到了标题,虽然我没有任何损坏要显示。

有没有办法只在if statement内做一次,比如:

{% for damage in car.damages %}
       {% if damage.images|length > 0 %}
          <div>Damages with the photos</div> // Show it only once when it comes here
          // Show the damages with photos
       {% endif %}
{% endfor %}

{% for damage in car.damages %}
       {% if damage.images|length == 0 %}
          <div>Damages without the photos</div> // Show it only once when it comes here
          // Show the damages without photos
       {% endif %}
{% endfor %}

知道怎么解决吗?

2 个答案:

答案 0 :(得分:1)

我不再熟悉Django和Python,但我想你可以做一个初步的检查,看看是否有图像损坏。

damages_with_images = [d for d in damages if d.images.length > 0]
damages_without_images  = [d for d in damages if d.images.length == 0]

然后只循环遍历这两个独立的数组,只打印标题,如果它们不是空的......

{% if damages_with_images|length > 0 %}
  put heading1
{% endif %}
# ... loop ...

{% if damages_without_images|length > 0 %}
  put heading2
{% endif %}
# ... loop ...

当然,由于存在多个循环,因此性能更差。

答案 1 :(得分:0)

我的职责是

  1. 在模板中传递两个单独的QuerySet,第一个包含图像损坏和其他没有图像的损坏。

  2. 保持QuerySet不变,并创建两个自定义模板过滤器,与上面的步骤#1完成相同的工作。

  3. 案例1:

    # views.py
    
    def my_view(request):
        damages_w_img = Damage.objects.filter(images__isnull=False)
        damages_wo_img = Damage.objects.filter(images__isnull=True)
        return render(request, 'template.html', locals())
    
    
    <!-- template.html -->
    
    {% if damages_w_img.exists %}
        <h1>This list of damages contains images</h1>
        {% for damage in damages_w_img %}
            do stuff here, each damage has an image
        {% endfor %}
    {% endif %}
    
    {% if damages_wo_img.exists %}
        <h1>This list of damages does not contain images</h1>
        {% for damage in damages_wo_img %}
            do stuff here, each damage does not has an image
        {% endfor %}
    {% endif %}
    

    案例2:

    # custom_template_filter.py
    
    from django import template
    
    register = template.Library()
    
    @register.filter
    def with_images(damages):
        return damages.objects.filter(images__isnull=False)
    
    @register.filter
    def without_images(damages):
        return damages.objects.filter(images__isnull=True)
    
    
    <!-- template.html -->
    
    {% with damage_w_img=car.damages|with_images damage_wo_img=car.damages|without_images %}
    
        {% if damages_w_img.exists %}
            <h1>This list of damages contains images</h1>
            {% for damage in damages_w_img %}
                do stuff here, each damage has an image
            {% endfor %}
        {% endif %}
    
        {% if damages_wo_img.exists %}
            <h1>This list of damages does not contain images</h1>
            {% for damage in damages_wo_img %}
                do stuff here, each damage does not has an image
            {% endfor %}
        {% endif %}
    
    {% endwith %}