tags:

views:

83

answers:

1

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 where 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.

+4  A: 

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.

Eli Courtwright
Thanks, Eli! This definitely helps. I think I'm back on the track.
geoffa