Fixed: 3274: Added date_list context variable to the archive_month generic 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
This commit is contained in:
Karen Tracey 2010-01-10 19:52:17 +00:00
parent 9bb1fa7251
commit b45fd3ffdf
3 changed files with 27 additions and 0 deletions

View File

@ -105,6 +105,8 @@ def archive_month(request, year, month, queryset, date_field,
Templates: ``<app_label>/<model_name>_archive_month.html`` Templates: ``<app_label>/<model_name>_archive_month.html``
Context: Context:
date_list:
List of days in this month with objects
month: month:
(date) this month (date) this month
next_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: if last_day >= now.date() and not allow_future:
lookup_kwargs['%s__lte' % date_field] = now lookup_kwargs['%s__lte' % date_field] = now
object_list = queryset.filter(**lookup_kwargs) object_list = queryset.filter(**lookup_kwargs)
date_list = object_list.dates(date_field, 'day')
if not object_list and not allow_empty: if not object_list and not allow_empty:
raise Http404 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()) template_name = "%s/%s_archive_month.html" % (model._meta.app_label, model._meta.object_name.lower())
t = template_loader.get_template(template_name) t = template_loader.get_template(template_name)
c = RequestContext(request, { c = RequestContext(request, {
'date_list': date_list,
'%s_list' % template_object_name: object_list, '%s_list' % template_object_name: object_list,
'month': date, 'month': date,
'next_month': next_month, 'next_month': next_month,

View File

@ -369,8 +369,15 @@ If ``template_name`` isn't specified, this view will use the template
**Template context:** **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: 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. * ``month``: A ``datetime.date`` object representing the given month.
* ``next_month``: A ``datetime.date`` object representing the first day of * ``next_month``: A ``datetime.date`` object representing the first day of

View File

@ -111,6 +111,22 @@ class MonthArchiveTest(TestCase):
self.assertEqual(response.context['next_month'], None) self.assertEqual(response.context['next_month'], None)
self.assertEqual(response.context['previous_month'], prev_month) 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): class DayArchiveTests(TestCase):
def test_year_month_day_format(self): def test_year_month_day_format(self):