django 模板中的逗号分隔列表

2024-02-01

If fruits是列表['apples', 'oranges', 'pears'],

有没有一种使用 django 模板标签快速生成“苹果、橙子和梨”的方法?

I know it's not difficult to do this using a loop and {% if counter.last %} statements, but because I'm going to use this repeatedly I think I'm going to have to learn how to write custom tags filters, and I don't want to reinvent the wheel if it's already been done.

作为扩展,我尝试放弃牛津逗号 http://en.wikipedia.org/wiki/Serial_comma(即返回“苹果、橙子和梨”)甚至更加混乱。


第一个选择:使用现有的连接模板标签。

http://docs.djangoproject.com/en/dev/ref/templates/builtins/#join http://docs.djangoproject.com/en/dev/ref/templates/builtins/#join

这是他们的例子

{{ value|join:" // " }}

第二个选择:在视图中执行。

fruits_text = ", ".join( fruits )

Provide fruits_text到模板进行渲染。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

django 模板中的逗号分隔列表 的相关文章

随机推荐