django template turn array into HTML table

后端 未结 3 1045
悲哀的现实
悲哀的现实 2021-02-02 17:32

I have a list of 16 results, let\'s call it \"results\". I want to arrange them in a 4 x 4 table.

Using the django template, how can I do this? (It doesn\'t seem like cy

3条回答
  •  清歌不尽
    2021-02-02 17:42

    Suppose you have:
    results=[[1, 2, 3, 4,], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]

    The following template can be used to display results in a table:

    
      {% for rowval in results %}    
         
         {% for val in rowval %}
          
         {% endfor %}
        
      {% endfor %}
    
    {{val}}

    It will display:

    1  2  3  4
    5  6  7  8
    9  10 11 12
    13 14 15 16
    

提交回复
热议问题