views:

843

answers:

3

I want to make an archive_index page for my django site. However, the date-based generic views really aren't any help. I want the dictionary returned by the view to have all the years and months for which at least one instance of the object type exists. So if my blog started in September 2007, but there were no posts in April 2008, I could get something like this

2009 - Jan, Feb, Mar
2008 - Jan, Feb, Mar, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec
2007 - Sep, Oct, Nov, Dec
A: 

You should be able to get all the info you describe from the built-in views. Can you be more specific as to what you cannot get? This should have everything you need:

django.views.generic.date_based.archive_month

Reference page (search for the above string on that page)

jcoon
No, that gives the actual objects for the month. I don't want the objects. I just want the distinct months and years themselves.
Apreche
+5  A: 

This will give you a list of unique posting dates:

Posts.objects.filter(draft=False).dates('post_date','month',order='DESC')

Of course you might not need the draft filter, and change 'post_date' to your field name, etc.

Van Gale
+2  A: 

I found the answer to my own question.

It's on this page in the documentation.

There's a function called dates that will give you distinct dates. So I can do

Entry.objects.dates('pub_date','month') to get a list of datetime objects, one for each year/month.

Apreche
That's funny you can answer your own question. I would add your findings to your original post though, too.
jcoon