Ticket #15272: patch2.diff
File patch2.diff, 15.0 KB (added by , 14 years ago) |
---|
-
docs/ref/class-based-views.txt
162 162 it constructs a :class:`QuerySet` by calling the `all()` method on the 163 163 :attr:`~SingleObjectMixin.model` attribute's default manager. 164 164 165 .. method:: get_context_object_name(obj ect_list)165 .. method:: get_context_object_name(obj) 166 166 167 167 Return the context variable name that will be used to contain the 168 list of data that this view is manipulating. If ``object_list`` is a 169 :class:`QuerySet` of Django objects and 168 data that this view is manipulating. If 170 169 :attr:`~SingleObjectMixin.context_object_name` is not set, the context 171 name will be constructed from the verbose plural name of the model that 172 the queryset is composed from. 170 name will be constructed from the lowercased name of the model that 171 the queryset is composed from. So for the model ``Article`` context 172 object name would be ``'article'``. 173 173 174 174 .. method:: get_context_data(**kwargs) 175 175 … … 334 334 .. method:: get_context_object_name(object_list) 335 335 336 336 Return the context variable name that will be used to contain the list 337 of data that this view is manipulating. If object_list is a queryset of 338 Django objects, the context name will be verbose plural name of the 339 model that the queryset is composed from. 337 of data that this view is manipulating. If ``object_list`` is a 338 queryset of Django objects and 339 :attr:`~MultipleObjectMixin.context_object_name` is not set, the context 340 name will be lowercased name of the model that the queryset is composed 341 from with postfix ``'_list'``. So for the model ``Article`` context 342 object name would be ``article_list``. 340 343 341 344 .. method:: get_context_data(**kwargs) 342 345 -
tests/regressiontests/generic_views/list.py
19 19 self.assertEqual(res.status_code, 200) 20 20 self.assertTemplateUsed(res, 'generic_views/author_list.html') 21 21 self.assertEqual(list(res.context['object_list']), list(Author.objects.all())) 22 self.assertIs(res.context['author s'], res.context['object_list'])22 self.assertIs(res.context['author_list'], res.context['object_list']) 23 23 self.assertIsNone(res.context['paginator']) 24 24 self.assertIsNone(res.context['page_obj']) 25 25 self.assertFalse(res.context['is_paginated']) … … 30 30 self.assertEqual(res.status_code, 200) 31 31 self.assertTemplateUsed(res, 'generic_views/author_list.html') 32 32 self.assertEqual(len(res.context['object_list']), 30) 33 self.assertIs(res.context['author s'], res.context['object_list'])33 self.assertIs(res.context['author_list'], res.context['object_list']) 34 34 self.assertTrue(res.context['is_paginated']) 35 35 self.assertEqual(res.context['page_obj'].number, 1) 36 36 self.assertEqual(res.context['paginator'].num_pages, 4) 37 self.assertEqual(res.context['author s'][0].name, 'Author 00')38 self.assertEqual(list(res.context['author s'])[-1].name, 'Author 29')37 self.assertEqual(res.context['author_list'][0].name, 'Author 00') 38 self.assertEqual(list(res.context['author_list'])[-1].name, 'Author 29') 39 39 40 40 def test_paginated_queryset_shortdata(self): 41 41 # Test that short datasets ALSO result in a paginated view. … … 43 43 self.assertEqual(res.status_code, 200) 44 44 self.assertTemplateUsed(res, 'generic_views/author_list.html') 45 45 self.assertEqual(list(res.context['object_list']), list(Author.objects.all())) 46 self.assertIs(res.context['author s'], res.context['object_list'])46 self.assertIs(res.context['author_list'], res.context['object_list']) 47 47 self.assertEqual(res.context['page_obj'].number, 1) 48 48 self.assertEqual(res.context['paginator'].num_pages, 1) 49 49 self.assertTrue(res.context['is_paginated']) … … 54 54 self.assertEqual(res.status_code, 200) 55 55 self.assertTemplateUsed(res, 'generic_views/author_list.html') 56 56 self.assertEqual(len(res.context['object_list']), 30) 57 self.assertIs(res.context['author s'], res.context['object_list'])58 self.assertEqual(res.context['author s'][0].name, 'Author 30')57 self.assertIs(res.context['author_list'], res.context['object_list']) 58 self.assertEqual(res.context['author_list'][0].name, 'Author 30') 59 59 self.assertEqual(res.context['page_obj'].number, 2) 60 60 61 61 def test_paginated_get_last_page_by_query_string(self): … … 63 63 res = self.client.get('/list/authors/paginated/', {'page': 'last'}) 64 64 self.assertEqual(res.status_code, 200) 65 65 self.assertEqual(len(res.context['object_list']), 10) 66 self.assertIs(res.context['author s'], res.context['object_list'])67 self.assertEqual(res.context['author s'][0].name, 'Author 90')66 self.assertIs(res.context['author_list'], res.context['object_list']) 67 self.assertEqual(res.context['author_list'][0].name, 'Author 90') 68 68 self.assertEqual(res.context['page_obj'].number, 4) 69 69 70 70 def test_paginated_get_page_by_urlvar(self): … … 73 73 self.assertEqual(res.status_code, 200) 74 74 self.assertTemplateUsed(res, 'generic_views/author_list.html') 75 75 self.assertEqual(len(res.context['object_list']), 30) 76 self.assertIs(res.context['author s'], res.context['object_list'])77 self.assertEqual(res.context['author s'][0].name, 'Author 60')76 self.assertIs(res.context['author_list'], res.context['object_list']) 77 self.assertEqual(res.context['author_list'][0].name, 'Author 60') 78 78 self.assertEqual(res.context['page_obj'].number, 3) 79 79 80 80 def test_paginated_page_out_of_range(self): … … 112 112 self.assertEqual(res.status_code, 200) 113 113 self.assertTemplateUsed(res, 'generic_views/list.html') 114 114 self.assertEqual(list(res.context['object_list']), list(Artist.objects.all())) 115 self.assertIs(res.context[' professional_artists'], res.context['object_list'])115 self.assertIs(res.context['artist_list'], res.context['object_list']) 116 116 self.assertIsNone(res.context['paginator']) 117 117 self.assertIsNone(res.context['page_obj']) 118 118 self.assertFalse(res.context['is_paginated']) … … 128 128 res = self.client.get('/list/authors/template_name/') 129 129 self.assertEqual(res.status_code, 200) 130 130 self.assertEqual(list(res.context['object_list']), list(Author.objects.all())) 131 self.assertIs(res.context['author s'], res.context['object_list'])131 self.assertIs(res.context['author_list'], res.context['object_list']) 132 132 self.assertTemplateUsed(res, 'generic_views/list.html') 133 133 134 134 def test_template_name_suffix(self): 135 135 res = self.client.get('/list/authors/template_name_suffix/') 136 136 self.assertEqual(res.status_code, 200) 137 137 self.assertEqual(list(res.context['object_list']), list(Author.objects.all())) 138 self.assertIs(res.context['author s'], res.context['object_list'])138 self.assertIs(res.context['author_list'], res.context['object_list']) 139 139 self.assertTemplateUsed(res, 'generic_views/author_objects.html') 140 140 141 141 def test_context_object_name(self): -
tests/regressiontests/generic_views/dates.py
92 92 res = self.client.get('/dates/books/2006/make_object_list/') 93 93 self.assertEqual(res.status_code, 200) 94 94 self.assertEqual(list(res.context['date_list']), [datetime.datetime(2006, 5, 1)]) 95 self.assertEqual(list(res.context['book s']), list(Book.objects.filter(pubdate__year=2006)))95 self.assertEqual(list(res.context['book_list']), list(Book.objects.filter(pubdate__year=2006))) 96 96 self.assertEqual(list(res.context['object_list']), list(Book.objects.filter(pubdate__year=2006))) 97 97 self.assertTemplateUsed(res, 'generic_views/book_archive_year.html') 98 98 … … 102 102 res = self.client.get('/dates/books/1999/allow_empty/') 103 103 self.assertEqual(res.status_code, 200) 104 104 self.assertEqual(list(res.context['date_list']), []) 105 self.assertEqual(list(res.context['book s']), [])105 self.assertEqual(list(res.context['book_list']), []) 106 106 107 107 def test_year_view_allow_future(self): 108 108 # Create a new book in the future … … 113 113 114 114 res = self.client.get('/dates/books/%s/allow_empty/' % year) 115 115 self.assertEqual(res.status_code, 200) 116 self.assertEqual(list(res.context['book s']), [])116 self.assertEqual(list(res.context['book_list']), []) 117 117 118 118 res = self.client.get('/dates/books/%s/allow_future/' % year) 119 119 self.assertEqual(res.status_code, 200) … … 132 132 self.assertEqual(res.status_code, 200) 133 133 self.assertTemplateUsed(res, 'generic_views/book_archive_month.html') 134 134 self.assertEqual(list(res.context['date_list']), [datetime.datetime(2008, 10, 1)]) 135 self.assertEqual(list(res.context['book s']),135 self.assertEqual(list(res.context['book_list']), 136 136 list(Book.objects.filter(pubdate=datetime.date(2008, 10, 1)))) 137 137 self.assertEqual(res.context['month'], datetime.date(2008, 10, 1)) 138 138 … … 149 149 res = self.client.get('/dates/books/2000/jan/allow_empty/') 150 150 self.assertEqual(res.status_code, 200) 151 151 self.assertEqual(list(res.context['date_list']), []) 152 self.assertEqual(list(res.context['book s']), [])152 self.assertEqual(list(res.context['book_list']), []) 153 153 self.assertEqual(res.context['month'], datetime.date(2000, 1, 1)) 154 154 155 155 # Since it's allow empty, next/prev are allowed to be empty months (#7164) … … 175 175 res = self.client.get('/dates/books/%s/allow_future/' % urlbit) 176 176 self.assertEqual(res.status_code, 200) 177 177 self.assertEqual(res.context['date_list'][0].date(), b.pubdate) 178 self.assertEqual(list(res.context['book s']), [b])178 self.assertEqual(list(res.context['book_list']), [b]) 179 179 self.assertEqual(res.context['month'], future) 180 180 181 181 # Since it's allow_future but not allow_empty, next/prev are not … … 229 229 res = self.client.get('/dates/books/2008/week/39/') 230 230 self.assertEqual(res.status_code, 200) 231 231 self.assertTemplateUsed(res, 'generic_views/book_archive_week.html') 232 self.assertEqual(res.context['book s'][0], Book.objects.get(pubdate=datetime.date(2008, 10, 1)))232 self.assertEqual(res.context['book_list'][0], Book.objects.get(pubdate=datetime.date(2008, 10, 1))) 233 233 self.assertEqual(res.context['week'], datetime.date(2008, 9, 28)) 234 234 235 235 def test_week_view_allow_empty(self): … … 238 238 239 239 res = self.client.get('/dates/books/2008/week/12/allow_empty/') 240 240 self.assertEqual(res.status_code, 200) 241 self.assertEqual(list(res.context['book s']), [])241 self.assertEqual(list(res.context['book_list']), []) 242 242 243 243 def test_week_view_allow_future(self): 244 244 future = datetime.date(datetime.date.today().year + 1, 1, 1) … … 249 249 250 250 res = self.client.get('/dates/books/%s/week/1/allow_future/' % future.year) 251 251 self.assertEqual(res.status_code, 200) 252 self.assertEqual(list(res.context['book s']), [b])252 self.assertEqual(list(res.context['book_list']), [b]) 253 253 254 254 def test_week_view_invalid_pattern(self): 255 255 res = self.client.get('/dates/books/2007/week/no_week/') … … 273 273 res = self.client.get('/dates/books/2008/oct/01/') 274 274 self.assertEqual(res.status_code, 200) 275 275 self.assertTemplateUsed(res, 'generic_views/book_archive_day.html') 276 self.assertEqual(list(res.context['book s']),276 self.assertEqual(list(res.context['book_list']), 277 277 list(Book.objects.filter(pubdate=datetime.date(2008, 10, 1)))) 278 278 self.assertEqual(res.context['day'], datetime.date(2008, 10, 1)) 279 279 … … 289 289 # allow_empty = True, empty month 290 290 res = self.client.get('/dates/books/2000/jan/1/allow_empty/') 291 291 self.assertEqual(res.status_code, 200) 292 self.assertEqual(list(res.context['book s']), [])292 self.assertEqual(list(res.context['book_list']), []) 293 293 self.assertEqual(res.context['day'], datetime.date(2000, 1, 1)) 294 294 295 295 # Since it's allow empty, next/prev are allowed to be empty months (#7164) … … 314 314 # allow_future = True, valid future month 315 315 res = self.client.get('/dates/books/%s/allow_future/' % urlbit) 316 316 self.assertEqual(res.status_code, 200) 317 self.assertEqual(list(res.context['book s']), [b])317 self.assertEqual(list(res.context['book_list']), [b]) 318 318 self.assertEqual(res.context['day'], future) 319 319 320 320 # allow_future but not allow_empty, next/prev amust be valid -
tests/regressiontests/generic_views/detail.py
32 32 res = self.client.get('/detail/artist/1/') 33 33 self.assertEqual(res.status_code, 200) 34 34 self.assertEqual(res.context['object'], Artist.objects.get(pk=1)) 35 self.assertEqual(res.context[' professional_artist'], Artist.objects.get(pk=1))35 self.assertEqual(res.context['artist'], Artist.objects.get(pk=1)) 36 36 self.assertTemplateUsed(res, 'generic_views/artist_detail.html') 37 37 38 38 def test_template_name(self): -
django/views/generic/list.py
76 76 if self.context_object_name: 77 77 return self.context_object_name 78 78 elif hasattr(object_list, 'model'): 79 return smart_str(re.sub('[^a-zA-Z0-9]+', '_', 80 object_list.model._meta.verbose_name_plural.lower())) 79 return smart_str('%s_list' % object_list.model._meta.object_name.lower()) 81 80 else: 82 81 return None 83 82 -
django/views/generic/detail.py
80 80 if self.context_object_name: 81 81 return self.context_object_name 82 82 elif hasattr(obj, '_meta'): 83 return smart_str(re.sub('[^a-zA-Z0-9]+', '_', 84 obj._meta.verbose_name.lower())) 83 return smart_str(obj._meta.object_name.lower()) 85 84 else: 86 85 return None 87 86