Ticket #3274: month_archive_date_list.diff

File month_archive_date_list.diff, 3.2 KB (added by seanbrant, 6 years ago)
  • django/views/generic/date_based.py

     
    105105
    106106    Templates: ``<app_label>/<model_name>_archive_month.html``
    107107    Context:
     108        date_list:
     109            List of days in this month with objects
    108110        month:
    109111            (date) this month
    110112        next_month:
     
    139141    if last_day >= now.date() and not allow_future:
    140142        lookup_kwargs['%s__lte' % date_field] = now
    141143    object_list = queryset.filter(**lookup_kwargs)
     144    date_list = object_list.dates(date_field, 'day')
    142145    if not object_list and not allow_empty:
    143146        raise Http404
    144147
     
    160163        template_name = "%s/%s_archive_month.html" % (model._meta.app_label, model._meta.object_name.lower())
    161164    t = template_loader.get_template(template_name)
    162165    c = RequestContext(request, {
     166        'date_list': date_list,
    163167        '%s_list' % template_object_name: object_list,
    164168        'month': date,
    165169        'next_month': next_month,
  • tests/regressiontests/views/tests/generic/date_based.py

     
    110110        self.assertEqual(response.status_code, 200)
    111111        self.assertEqual(response.context['next_month'], None)
    112112        self.assertEqual(response.context['previous_month'], prev_month)
     113       
     114    def test_archive_month_date_list(self):
     115        author = Author(name="John Smith")
     116        author.save()
     117        date1 = datetime(2010, 1, 1, 0, 0, 0)
     118        date2 = datetime(2010, 1, 2, 0, 0, 0)
     119        Article.objects.create(title='example1', author=author, date_created=date1)
     120        Article.objects.create(title='example2', author=author, date_created=date2)
     121        response = self.client.get('/views/date_based/archive_month/2010/1/')
     122        self.assertEqual(response.status_code, 200)
     123        self.assertEqual(len(response.context['date_list']), 2)
     124        self.assertEqual(response.context['date_list'][0], date1)
     125        # Checks that the same date is not included more than once in the list
     126        Article.objects.create(title='example2', author=author, date_created=date2)
     127        response = self.client.get('/views/date_based/archive_month/2010/1/')
     128        self.assertEqual(len(response.context['date_list']), 2)
    113129
    114130class DayArchiveTests(TestCase):
    115131
  • docs/ref/generic-views.txt

     
    371371
    372372In addition to ``extra_context``, the template's context will be:
    373373
     374.. versionadded:: 1.2
     375
     376    * ``date_list``: A list of ``datetime.date`` objects representing all
     377      days that have objects available in the given month, according to
     378      ``queryset``, in ascending order.   
     379
    374380    * ``month``: A ``datetime.date`` object representing the given month.
    375381
    376382    * ``next_month``: A ``datetime.date`` object representing the first day of
Back to Top