【问题标题】:Django iterate queryset just n number of times in templatesDjango 在模板中仅迭代查询集 n 次
【发布时间】:2020-07-03 02:46:30
【问题描述】:

我的问题很简单。我正在使用 Django taggit。

我只想迭代 2 次。意味着在模板中只显示 4 个标签。

 {% for tag in data.tags.all %}
     {{tag}}                            
 {% endfor %}

我试过了,但没有任何意义:

 {% for tag in data.tags.all|ljust:"2" %}
    {{tag}}                            
 {% endfor %}

谁能建议我如何实现它?

【问题讨论】:

    标签: python django django-taggit


    【解决方案1】:

    您可以使用|slice template filter [Django-doc]:

    {% for tag in data.tags.all|slice:':2' %}
        {{ tag }}                            
    {% endfor %}

    【讨论】:

      猜你喜欢
      • 2016-09-13
      • 2019-02-05
      • 1970-01-01
      • 2017-10-13
      • 2019-09-14
      • 2017-02-24
      • 2016-06-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多