Skip to main content
 首页 » 编程设计

python之在 Django 模板中将列表转换为逗号分隔值

2025年05月04日34lyj

在 View 中我可以做:

l = ['one', 'two', 'three'] 
', '.join(l) 
>>> 'one, two, three' 

有没有办法在模板中执行相同的操作,或者我是否需要创建自定义模板过滤器?

请您参考如下方法:

为此有一个内置过滤器。来自docs

Filter arguments that contain spaces must be quoted; for example, to join a list with commas and spaces you’d use {{ list|join:", " }}.