diff --git a/django/views/generic/date_based.py b/django/views/generic/date_based.py index 0d9ecaca327..726df59654e 100644 --- a/django/views/generic/date_based.py +++ b/django/views/generic/date_based.py @@ -105,6 +105,8 @@ def archive_month(request, year, month, queryset, date_field, Templates: ``/_archive_month.html`` Context: + date_list: + List of days in this month with objects month: (date) this month next_month: @@ -139,6 +141,7 @@ def archive_month(request, year, month, queryset, date_field, if last_day >= now.date() and not allow_future: lookup_kwargs['%s__lte' % date_field] = now object_list = queryset.filter(**lookup_kwargs) + date_list = object_list.dates(date_field, 'day') if not object_list and not allow_empty: raise Http404 @@ -160,6 +163,7 @@ def archive_month(request, year, month, queryset, date_field, template_name = "%s/%s_archive_month.html" % (model._meta.app_label, model._meta.object_name.lower()) t = template_loader.get_template(template_name) c = RequestContext(request, { + 'date_list': date_list, '%s_list' % template_object_name: object_list, 'month': date, 'next_month': next_month, diff --git a/docs/ref/generic-views.txt b/docs/ref/generic-views.txt index 9824dcdb8f9..574db88a603 100644 --- a/docs/ref/generic-views.txt +++ b/docs/ref/generic-views.txt @@ -369,8 +369,15 @@ If ``template_name`` isn't specified, this view will use the template **Template context:** +.. versionadded:: 1.2 + The inclusion of ``date_list`` in the template's context is new. + In addition to ``extra_context``, the template's context will be: + * ``date_list``: A list of ``datetime.date`` objects representing all + days that have objects available in the given month, according to + ``queryset``, in ascending order. + * ``month``: A ``datetime.date`` object representing the given month. * ``next_month``: A ``datetime.date`` object representing the first day of diff --git a/tests/regressiontests/views/tests/generic/date_based.py b/tests/regressiontests/views/tests/generic/date_based.py index 2ca1bfd090f..c6ba56204e5 100644 --- a/tests/regressiontests/views/tests/generic/date_based.py +++ b/tests/regressiontests/views/tests/generic/date_based.py @@ -110,6 +110,22 @@ class MonthArchiveTest(TestCase): self.assertEqual(response.status_code, 200) self.assertEqual(response.context['next_month'], None) self.assertEqual(response.context['previous_month'], prev_month) + + def test_archive_month_date_list(self): + author = Author(name="John Smith") + author.save() + date1 = datetime(2010, 1, 1, 0, 0, 0) + date2 = datetime(2010, 1, 2, 0, 0, 0) + Article.objects.create(title='example1', author=author, date_created=date1) + Article.objects.create(title='example2', author=author, date_created=date2) + response = self.client.get('/views/date_based/archive_month/2010/1/') + self.assertEqual(response.status_code, 200) + self.assertEqual(len(response.context['date_list']), 2) + self.assertEqual(response.context['date_list'][0], date1) + # Checks that the same date is not included more than once in the list + Article.objects.create(title='example2', author=author, date_created=date2) + response = self.client.get('/views/date_based/archive_month/2010/1/') + self.assertEqual(len(response.context['date_list']), 2) class DayArchiveTests(TestCase):