Django template for loop and display first X matches

后端 未结 3 565
忘了有多久
忘了有多久 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:36

    You could have used slice if there isn't {% if category.reward_type == reward_type %}.

    i.e. if you have category_matching_rewards which is list having category.reward_type == reward_type then in template it can be done as

    {%for category in category_matching_rewards|slice:"4" %}
           Show catgory
           ....
    {%endfor%}
    

提交回复
热议问题