浏览代码

Fixed #15698 -- Fixed inconsistant handling of context_object_name in paginated MultipleObjectMixin views. Thanks, Dave Hall.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16079 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Jannis Leidel 14 年之前
父节点
当前提交
598032b8c4

+ 1 - 1
django/views/generic/list.py

@@ -89,6 +89,7 @@ class MultipleObjectMixin(object):
         """
         queryset = kwargs.pop('object_list')
         page_size = self.get_paginate_by(queryset)
+        context_object_name = self.get_context_object_name(queryset)
         if page_size:
             paginator, page, queryset, is_paginated = self.paginate_queryset(queryset, page_size)
             context = {
@@ -105,7 +106,6 @@ class MultipleObjectMixin(object):
                 'object_list': queryset
             }
         context.update(kwargs)
-        context_object_name = self.get_context_object_name(queryset)
         if context_object_name is not None:
             context[context_object_name] = queryset
         return context

+ 30 - 0
tests/regressiontests/generic_views/dates.py

@@ -119,6 +119,13 @@ class YearArchiveViewTests(TestCase):
         self.assertEqual(res.status_code, 200)
         self.assertEqual(list(res.context['date_list']), [datetime.datetime(year, 1, 1)])
 
+    def test_year_view_paginated(self):
+        res = self.client.get('/dates/books/2006/paginated/')
+        self.assertEqual(res.status_code, 200)
+        self.assertEqual(list(res.context['book_list']), list(Book.objects.filter(pubdate__year=2006)))
+        self.assertEqual(list(res.context['object_list']), list(Book.objects.filter(pubdate__year=2006)))
+        self.assertTemplateUsed(res, 'generic_views/book_archive_year.html')
+
     def test_year_view_invalid_pattern(self):
         res = self.client.get('/dates/books/no_year/')
         self.assertEqual(res.status_code, 404)
@@ -190,6 +197,13 @@ class MonthArchiveViewTests(TestCase):
         self.assertEqual(res.context['next_month'], future)
         self.assertEqual(res.context['previous_month'], datetime.date(2006, 5, 1))
 
+    def test_month_view_paginated(self):
+        res = self.client.get('/dates/books/2008/oct/paginated/')
+        self.assertEqual(res.status_code, 200)
+        self.assertEqual(list(res.context['book_list']), list(Book.objects.filter(pubdate__year=2008, pubdate__month=10)))
+        self.assertEqual(list(res.context['object_list']), list(Book.objects.filter(pubdate__year=2008, pubdate__month=10)))
+        self.assertTemplateUsed(res, 'generic_views/book_archive_month.html')
+
     def test_custom_month_format(self):
         res = self.client.get('/dates/books/2008/10/')
         self.assertEqual(res.status_code, 200)
@@ -251,6 +265,15 @@ class WeekArchiveViewTests(TestCase):
         self.assertEqual(res.status_code, 200)
         self.assertEqual(list(res.context['book_list']), [b])
 
+    def test_week_view_paginated(self):
+        week_start = datetime.date(2008, 9, 28)
+        week_end = week_start + datetime.timedelta(days=7)
+        res = self.client.get('/dates/books/2008/week/39/')
+        self.assertEqual(res.status_code, 200)
+        self.assertEqual(list(res.context['book_list']), list(Book.objects.filter(pubdate__gte=week_start, pubdate__lt=week_end)))
+        self.assertEqual(list(res.context['object_list']), list(Book.objects.filter(pubdate__gte=week_start, pubdate__lt=week_end)))
+        self.assertTemplateUsed(res, 'generic_views/book_archive_week.html')
+
     def test_week_view_invalid_pattern(self):
         res = self.client.get('/dates/books/2007/week/no_week/')
         self.assertEqual(res.status_code, 404)
@@ -327,6 +350,13 @@ class DayArchiveViewTests(TestCase):
         self.assertEqual(res.context['next_day'], future)
         self.assertEqual(res.context['previous_day'], datetime.date(2006, 5, 1))
 
+    def test_day_view_paginated(self):
+        res = self.client.get('/dates/books/2008/oct/1/')
+        self.assertEqual(res.status_code, 200)
+        self.assertEqual(list(res.context['book_list']), list(Book.objects.filter(pubdate__year=2008, pubdate__month=10, pubdate__day=1)))
+        self.assertEqual(list(res.context['object_list']), list(Book.objects.filter(pubdate__year=2008, pubdate__month=10, pubdate__day=1)))
+        self.assertTemplateUsed(res, 'generic_views/book_archive_day.html')
+
     def test_next_prev_context(self):
         res = self.client.get('/dates/books/2008/oct/01/')
         self.assertEqual(res.content, "Archive for Oct. 1, 2008. Previous day is May 1, 2006")

+ 8 - 0
tests/regressiontests/generic_views/urls.py

@@ -146,6 +146,8 @@ urlpatterns = patterns('',
         views.BookYearArchive.as_view(allow_empty=True)),
     (r'^dates/books/(?P<year>\d{4})/allow_future/$',
         views.BookYearArchive.as_view(allow_future=True)),
+    (r'^dates/books/(?P<year>\d{4})/paginated/$',
+        views.BookYearArchive.as_view(make_object_list=True, paginate_by=30)),
     (r'^dates/books/no_year/$',
         views.BookYearArchive.as_view()),
 
@@ -158,6 +160,8 @@ urlpatterns = patterns('',
         views.BookMonthArchive.as_view(allow_empty=True)),
     (r'^dates/books/(?P<year>\d{4})/(?P<month>[a-z]{3})/allow_future/$',
         views.BookMonthArchive.as_view(allow_future=True)),
+    (r'^dates/books/(?P<year>\d{4})/(?P<month>[a-z]{3})/paginated/$',
+        views.BookMonthArchive.as_view(paginate_by=30)),
     (r'^dates/books/(?P<year>\d{4})/no_month/$',
         views.BookMonthArchive.as_view()),
 
@@ -168,6 +172,8 @@ urlpatterns = patterns('',
         views.BookWeekArchive.as_view(allow_empty=True)),
     (r'^dates/books/(?P<year>\d{4})/week/(?P<week>\d{1,2})/allow_future/$',
         views.BookWeekArchive.as_view(allow_future=True)),
+    (r'^dates/books/(?P<year>\d{4})/week/(?P<week>\d{1,2})/paginated/$',
+        views.BookWeekArchive.as_view(paginate_by=30)),
     (r'^dates/books/(?P<year>\d{4})/week/no_week/$',
         views.BookWeekArchive.as_view()),
     (r'^dates/books/(?P<year>\d{4})/week/(?P<week>\d{1,2})/monday/$',
@@ -182,6 +188,8 @@ urlpatterns = patterns('',
         views.BookDayArchive.as_view(allow_empty=True)),
     (r'^dates/books/(?P<year>\d{4})/(?P<month>[a-z]{3})/(?P<day>\d{1,2})/allow_future/$',
         views.BookDayArchive.as_view(allow_future=True)),
+    (r'^dates/books/(?P<year>\d{4})/(?P<month>[a-z]{3})/(?P<day>\d{1,2})/paginated/$',
+        views.BookDayArchive.as_view(paginate_by=True)),
     (r'^dates/books/(?P<year>\d{4})/(?P<month>[a-z]{3})/no_day/$',
         views.BookDayArchive.as_view()),