Comma separated lists in django templates

后端 未结 11 1377
梦如初夏
梦如初夏 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条回答
  •  你的背包
    2021-01-30 16:13

    I would simply use ', '.join(['apples', 'oranges', 'pears']) before sending it to the template as a context data.

    UPDATE:

    data = ['apples', 'oranges', 'pears']
    print(', '.join(data[0:-1]) + ' and ' + data[-1])
    

    You will get apples, oranges and pears output.

提交回复
热议问题