django模板列表变量中的最后一个元素 [英] Last element in django template list variable

查看:97
本文介绍了django模板列表变量中的最后一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何从上下文对象中过滤掉列表变量的最后一个元素。

I would like to know how to filter out the last element of a list variable from the context object.

{% for d in data %}
    {{ d }},
{% endfor %}

我不想在最后一个元素后面有。谢谢。

I don't want to have the , after the last element. Thank you.

注意:这只是一个假设的例子。我知道我们可以使用连接过滤器在这里实现同样的事情

NOTE: This is just a hypothetical example. I know we can use the join filter to achieve the same thing here

推荐答案

你的意思是 -

{% for d in data %}
    {% if forloop.last %}
        {{ d }}
    {% else %}
        {{ d }},
    {% endif %}
{% endfor %}

看看 django docs 上的模板循环

这篇关于django模板列表变量中的最后一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆