0

I have this function I was using to convert date objects from my blog posts into a dictionary of years and months I can display in a sidebar:

def post_dates(self):
    """Get dictionary of dates for all posts."""
    grouped_dates = groupby(Post.objects.datetimes('published', 'month'),
                            lambda date: date.year)

    def get_months(dates):
        return [{'name': months[d.month],
                 'num':str(d.month).zfill(2)} for d in dates]

    dates = [{"year": year, 'months': get_months(dates)}
             for year, dates in grouped_dates]
    return dates

Now I'm moving to use Generic Date Views all of which provide date_list on the context.

Currently a url for me looks like this:

 url(r'posts/(?P<year>\d{4})/?',
    YearArchiveView.as_view(model=Post, date_field="published"), name="post_year_archive"),

So I've not subclassed the view.

I really don't want to have to write a view for each. I thought you could do this in a context_processor but I've read you can't access current context from a contect processor.

How can I tweak my code to work for the Generic Date View without subclassing? If not let me know, and why?

Community
  • 1
  • 1
AncientSwordRage
  • 7,086
  • 19
  • 90
  • 173

0 Answers0