Comma separated lists in django templates

后端 未结 11 1214
梦如初夏
梦如初夏 2021-01-30 16:00

If fruits is the list [\'apples\', \'oranges\', \'pears\'],

is there a quick way using django template tags to produce \"apples, oranges, and p

11条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-30 16:22

    I would suggest a custom django templating filter rather than a custom tag -- filter is handier and simpler (where appropriate, like here). {{ fruits | joinby:", " }} looks like what I'd want to have for the purpose... with a custom joinby filter:

    def joinby(value, arg):
        return arg.join(value)
    

    which as you see is simplicity itself!

提交回复
热议问题