【问题标题】:How do i paginate objects grouping by their date in each page?如何在每页中按日期对对象进行分页?
【发布时间】:2021-12-14 01:08:49
【问题描述】:

尝试按日期对对象列表进行分页。

例如:

page 1 -> objects date is smaller than today
page 2 -> objects date is equals to today
page 3 -> objects date is equals to tomorrow 

等等。 每个页面可以有不同数量的元素。

【问题讨论】:

  • 我猜你想在你的模板中这样做
  • 实际上我想创建无限滚动并且每次我需要获取下一个日期

标签: django django-pagination


【解决方案1】:

来自 django 文档。

cities = [
    {'name': 'Mumbai', 'population': '19,000,000', 'country': 'India'},
    {'name': 'Calcutta', 'population': '15,000,000', 'country': 'India'},
    {'name': 'New York', 'population': '20,000,000', 'country': 'USA'},
    {'name': 'Chicago', 'population': '7,000,000', 'country': 'USA'},
    {'name': 'Tokyo', 'population': '33,000,000', 'country': 'Japan'},
]

{% regroup cities by country as country_list %}

<ul>
{% for country in country_list %}
    <li>{{ country.grouper }}
    <ul>
        {% for city in country.list %}
          <li>{{ city.name }}: {{ city.population }}</li>
        {% endfor %}
    </ul>
    </li>
{% endfor %}
</ul>

这正是您所需要的。 在此处阅读更多内容django regroup

【讨论】:

  • 感谢您的回复,但是我正在尝试创建无限滚动,并且在每次取回时我都需要在前几天取回。我发现我需要使用分页来创建无限滚动,但我猜 django 分页不支持通过在视图中分组进行分页
猜你喜欢
  • 2013-11-25
  • 1970-01-01
  • 2019-02-20
  • 2016-09-09
  • 2013-09-11
  • 2014-03-15
  • 1970-01-01
  • 1970-01-01
  • 2017-10-29
相关资源
最近更新 更多