【问题标题】:For all but the last in Django template [duplicate]除了Django模板中的最后一个之外的所有[重复]
【发布时间】:2018-03-22 02:31:56
【问题描述】:

有没有办法为 for 循环的所有运行做一些逻辑,而不是 Django 模板中的最后一个?

具体来说,如下所示:

{% for e in emails %}
    {{e.email_address}};
{% endfor %}

';'每次都包含在内,但最终我想在最后一次运行时忽略它。对于类似的语法任务,我有类似的用例。

【问题讨论】:

    标签: html django django-templates


    【解决方案1】:

    看看join的模板实现。

    {{ emails|join:"; " }}
    

    如果 emails 是列表 ['a', 'b', 'c'],则输出将是字符串“a; b; c”。

    【讨论】:

    • 这似乎是按字符打破的?
    • @NickP 在你的例子中你会做{{ emails|join:"; " }}。 (好吧,如果电子邮件是电子邮件列表,而不是您需要从中提取电子邮件的对象)。
    猜你喜欢
    • 1970-01-01
    • 2013-03-16
    • 2021-12-31
    • 2014-01-30
    • 2014-08-28
    • 2014-08-03
    • 2019-08-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多