Skip to content
Browse files

Fixed: 3274: Added date_list context variable to the archive_month ge…

…neric view, consistent with archive_index and archive_year. Thanks Sean Brant.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12195 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
1 parent 9bb1fa7 commit b45fd3ffdf2f238e0d16e56895e09c99f2ec50b1 @kmtracey kmtracey committed
View
4 django/views/generic/date_based.py
@@ -105,6 +105,8 @@ def archive_month(request, year, month, queryset, date_field,
Templates: ``<app_label>/<model_name>_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,
View
7 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
View
16 tests/regressiontests/views/tests/generic/date_based.py
@@ -110,6 +110,22 @@ def test_archive_month_includes_only_month(self):
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):

0 comments on commit b45fd3f

Please sign in to comment.
Something went wrong with that request. Please try again.