在Django中分组日期 [英] Grouping dates in Django
本文介绍了在Django中分组日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的问题与这篇文章几乎完全相同除了我使用Python和Django而不是PHP。
My question is almost exactly the same as this post, except that I'm using Python and Django instead of PHP.
任务是采取:
id date
1 2009-01-01 10:15:23
2 2009-01-01 13:21:29
3 2009-01-02 01:03:13
4 2009-01-03 12:20:19
5 2009-01-03 13:01:06
输出:
2009-01-01
1
2
2009-01-02
3
2009-01-03
4
5
我可以通过循环遍历排序的日期并将HTML输出到我的python视图文件中的一个字符串来手动地将其删除,但我想知道:有没有更好的方法使用Django模板或一个漂亮的Python功能?
I can manually hack something together by looping through the sorted dates and outputting HTML into a string in my python view file, but I'm wondering: is there's a better way using a Django template or a nifty Python feature?
推荐答案
您可以使用重组和日期模板标签解决此问题。
You can solve this with the regroup and date templatetags.
{% regroup object_list by start_date|date:"Y-m-d" as objects_by_day %}
{% for d in objects_by_day %}
### DO SOMETHING HERE
{% endfor %}
这篇关于在Django中分组日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文