[Answered ]-Django checkbox want to check only the first box

1👍

You could add the checked property to the second item as such:

{% ifequal forloop.counter 2 %} checked="checked"{% endifequal %}

The default forloop.counter is 1-indexed, or you can specifically use a 0-indexed counter:

forloop.counter0

1👍

The forloop variable set by the Django {% for %} tag is your friend here.

Pop this in:

{% if forloop.first %} checked="checked"{% endif %}

i.e.

{% for item in items %}
     <tr class="items_table_row">
        <td><input type="checkbox" name="{{item.pk}}" value="{{item.pk}}"{% if forloop.first %} checked="checked"{% endif %}></td>
        <td>{{item.tiptop_id}}</td><td>{{item.alternative_id}}</td><td>{{item.title}}</td><td>{{item.type}}</td><td>{{item.format}}</td>
        <td><span id="{{item.pk}}" name="type">{{item.itemstatushistory_set.latest}}</span></td><td>{{item.itemstatushistory_set.latest.date.date|date:"d M Y"}}</td>
        <td><a href="{% url tiptop.views.edit_item item.client.pk item.pk %}" onclick="return showAddAnotherPopup(this);">Edit</a></td>
    </tr>
{% endfor %}

Leave a comment