简体   繁体   中英

loop.last in jinja2 not working properly in django

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: &nbsp;</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: &nbsp;</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! &nbsp;</span>
                                            </div>
                                            {% endif %}
                                        </div>
                                    </td>
                                </tr>
                                <!-- 1st session ends -->
                                {% endfor %}

                            </tbody>
                        </table>
                    </div>

Thank you in advance for your help. Have a good day.

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 %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM