The DateDetailView
expects the format of year/month/day
As a general approach, if you will be using this kind of view in your project for other models than Mag alone, you can derive from it though and change the get_object
method, roughly like that:
class YearDetailView(DateDetailView):
def get_object(self, queryset=None):
"""
Get the object this request displays.
"""
year = self.get_year()
# Use a custom queryset if provided
qs = queryset or self.get_queryset()
if not self.get_allow_future() and int(year) > datetime.today().year:
raise Http404(_("Future %(verbose_name_plural)s not available because
%(class_name)s.allow_future is False.") % {
'verbose_name_plural': qs.model._meta.verbose_name_plural,
'class_name': self.__class__.__name__,
})
lookup_args = {"%s__year" % self.get_date_field(): year}
qs = qs.filter(**lookup_args)
return super(BaseDetailView, self).get_object(queryset=qs)
class MagDetail(YearDetailView): # note that we derive from our new class!
model = Mag
pk_url_kwarg='pk'
date_field='pub_date'