Django template for loop and display first X matches

后端 未结 3 563
忘了有多久
忘了有多久 2021-01-21 02:59

Not sure exactly how to phrase my problem but I essentially want to loop through a list and show the first 4 matches only.

{% for reward_type in reward_types %}         


        
3条回答
  •  逝去的感伤
    2021-01-21 03:30

    You may use forloop.counter, The for loop sets a number of variables available within the loop:

    forloop.counter The current iteration of the loop (1-indexed)

    forloop.counter0 The current iteration of the loop (0-indexed)

    forloop.revcounter The number of iterations from the end of the loop (1-indexed)

    forloop.revcounter0 The number of iterations from the end of the loop (0-indexed)

    forloop.first True if this is the first time through the loop

    forloop.last True if this is the last time through the loop

    forloop.parentloop For nested loops, this is the loop above the current one

    Source

提交回复
热议问题