【问题标题】:how to iterate list in django html template [duplicate]如何在django html模板中迭代列表[重复]
【发布时间】:2020-08-01 20:27:30
【问题描述】:

我有一个列表,我在 Django 的模板中放入表格:

<tr><th></th>{% for year in years %}<th>{{ year }}</th>{% endfor %}</tr>

years=[2020,2019,2018,2017,2016,2015,2014,2013....2002,2001,2000]

我只想从列表中打印过去 5 年

years=[2020,2019,2018,2017,2016]

所以我尝试了:

tr><th></th>{% for year in years %}<th>{{ year[:4] }}</th>{% endfor %}</tr>

但是这种方式是行不通的。如何从列表中仅迭代 5 年而不是 20 年。

【问题讨论】:

    标签: html django list


    【解决方案1】:
    {% for year in years|slice:":5" %}
    

    【讨论】:

      猜你喜欢
      • 2012-12-24
      • 2019-03-05
      • 1970-01-01
      • 2021-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-31
      • 2014-08-19
      相关资源
      最近更新 更多