Building a list in Django templates

后端 未结 7 1008
慢半拍i
慢半拍i 2020-12-05 13:14

With this code:

{% for o in [1,2,3] %}
    
{% cycle \'row1\' \'row2\' %}
{% endf
7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-05 13:33

    This maybe an inspiration. Use the buildin filter add.

    {{ first|add:second }}
    
    first is [1, 2, 3] and second is [4, 5, 6], then the output will be [1, 2, 3, 4, 5, 6].
    
    This filter will first try to coerce both values to integers. 
    If this fails, it'll attempt to add the values together anyway. 
    This will work on some data types (strings, list, etc.) and fail on others. 
    If it fails, the result will be an empty string.
    

    The official specification,https://docs.djangoproject.com/zh-hans/2.0/ref/templates/builtins/#built-in-filter-reference

提交回复
热议问题