简体   繁体   English

jinja2 中的 loop.last 在 django 中无法正常工作

[英]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.伙计们,我试图避免在我的 Django 模板循环的最后一次迭代中在我的表中打印两个 div。 I have used loop.last variable to check if the loop is in its last iteration, but it is not working for some reason.我使用 loop.last 变量来检查循环是否处于最后一次迭代中,但由于某种原因它无法正常工作。 Here program session is simply a range(number_of_iterations_required).这里的程序会话只是一个范围(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 .尝试将其更改为forloop而不是loop You can have a look at the django docs for more info您可以查看django 文档以获取更多信息

{% if not forloop.last %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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