Issue
guys I am trying to avoid printing two divs in my table in the last iteration of the loop of my Django template. I have used loop.last variable to check if the loop is in its last iteration, but it is not working for some reason. Here program session is simply a range(number_of_iterations_required). Here is my code:
{% for n in program_sessions %}
<!-- 1st session start -->
<tr class="mb-2">
<td class="border border-0">
<div class="row">
<div class="col-6 mx-0 px-0">
<span class="float-end">Row: </span>
</div>
<div class="col-6 mx-0 px-0">
<span class="text-white">{{program.workout_time}}m</span>
</div>
{% if not loop.last %}
<div class="col-6 mx-0 px-0">
<span class="float-end">Rest: </span>
</div>
<div class="col-6 mx-0 px-0">
<span class="text-white">{{program.rest_time}}min</span>
</div>
{% else %}
<div class="col-6 mx-0 px-0">
<span class="float-end">Last Iteration boii! </span>
</div>
{% endif %}
</div>
</td>
</tr>
<!-- 1st session ends -->
{% endfor %}
</tbody>
</table>
</div>
Thank you in advance for your help. Have a good day.
Solution
Looks like the syntax is not right. Try to change it to forloop instead of loop. You can have a look at the django docs for more info
{% if not forloop.last %}
Answered By - dstrants
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.