Patrick J. Anderson wrote: > Here's one of the inclusion tags I wrote this morning for my blog date > navigation: > > ----------------------------------------------------------------------- > > from django.template import Library,Node > from project.apps.news.models import Article > > register = Library() > > def articles_months(context): > if context['year']: > year = context['year'] > elif context['month']: > year = context['month'].strftime("%Y") > elif context['day']: > year = context['day'].strftime("%Y") > > months = Article.objects.filter(time_added__year = year, status = > 1, is_approved = True).dates( > 'time_added', 'month', order = 'DESC') > if months: > return { 'articles_months': months, } > > register.inclusion_tag('news/widgets/articles_months_menu.html', > takes_context = True)(articles_months) > > ------------------------------------------------------------------------- > > It works fine if I view the year archive, but once I try to view a > monthly archive, I receive an error: > > xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx > > Request Method: GET > Request URL: http://am.ws.local/news/2005/10/ > Exception Type: KeyError > Exception Value: 'year' > Exception Location: > /usr/lib/python2.4/site-packages/django/template/context.py in > __getitem__, line 40 > > xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx > > > What am I doing wrong? > > > > >
Found a better solution using context['object_list'] ---------------------------------------------------------------------------- from django.template import Library register = Library() def articles_months(context): output = [] for m in context['object_list']: output.append(m.time_added) if output: return { 'articles_months': output, } register.inclusion_tag('news/widgets/articles_months_menu.html', takes_context = True)(articles_months) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users -~----------~----~----~----~------~----~------~--~---