开发者

Display gaps in dates with Python and Django

开发者 https://www.devze.com 2022-12-13 05:47 出处:网络
I\'m building an application that requires each user to make a post on a daily basis. I\'d like to display the gaps in dates whe开发者_如何学Gore users haven\'t made posts. Since it doesn\'t seem like

I'm building an application that requires each user to make a post on a daily basis. I'd like to display the gaps in dates whe开发者_如何学Gore users haven't made posts. Since it doesn't seem like a good idea to insert empty database rows for empty posts, I'm only inserting a record when the user adds a post.

The model contains a field named log_date. I'm at a loss for how to display the gaps in the Django template. This is how I'd like the output in the template to look like:

2009-11-25
2009-11-24
2009-11-23 NO ENTRY
2009-11-22 NO ENTRY
2009-11-21

Thanks in advance for any help. Let me know if I can provide additional details.


I recommend building a set of dates that a user has posted, then start at the earliest date and iterate until the current day. For each day, print the date and check whether a post exists for that day. By using a set we can do this check in constant time, not that this will likely matter.

days = set( Post.objects.filter(user=TARGET).values_list("log_date", flat=True) )
curr = min(days)
while curr <= date.today():
    print curr,
    if curr not in days:
        print "NO ENTRY",
    print
    curr += timedelta(days=1)

I realize that this isn't Django template code, but this should give you a good starting point from which to build a good template.

0

精彩评论

暂无评论...
验证码 换一张
取 消