[Answer]-Template logic not functioning properly

1👍

You can create a variable inside views count_check_boxes that counts the number of choices the form have and test for it inside your template.

def type_list(request,type_id):
    ...
    place = TypeSelectionForm(type_id)
    count_check_boxes = len(place.fields['checkbox_field'].choices)
    return render(request, 'incident/type_list.html',
        {
            'about_menu': True,
            'type_list': type_list,
            'place':place,
            'check_boxes_count':check_boxes_count
    })

And in your template:

<tr><td>
    {% if place %}                                
      {%if check_boxes_count > 0%}
      <input type="checkbox" id="select_all"/>Display all<br />
      <hr style="width: 150px;margin: 8px 0;">
      {%endif%}
      {% for value in place %}
      {{ value }}
      {% endfor %}
    {% endif %}</td></tr>
<tr><td>{% include "buttons/addalist.html" %} {% include "buttons/save.html" %}</td></tr>

Hope this helps!

0👍

<tr><td>
    {% if place %}      
        {% for value in place %}
            {% if forloop.first %}
            <input type="checkbox" id="select_all"/>Display all<br />
            <hr style="width: 150px;margin: 8px 0;">
            {% endif %}
            {{ value }}
        {% endfor %}
    {% endif %}</td></tr>
<tr><td>{% include "buttons/addalist.html" %} {% include "buttons/save.html" %}</td></tr>

Using forloop.first checks if the for loop is in on its first iteration if so it’ll only display the input once per entire iteration.

Leave a comment