Ticket #15272: patch2.diff

File patch2.diff, 15.0 KB (added by szczav, 14 years ago)

fix, tests, docs

  • docs/ref/class-based-views.txt

     
    162162        it constructs a :class:`QuerySet` by calling the `all()` method on the
    163163        :attr:`~SingleObjectMixin.model` attribute's default manager.
    164164
    165     .. method:: get_context_object_name(object_list)
     165    .. method:: get_context_object_name(obj)
    166166
    167167        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
    170169        :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'``.
    173173
    174174    .. method:: get_context_data(**kwargs)
    175175
     
    334334    .. method:: get_context_object_name(object_list)
    335335
    336336        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``.
    340343
    341344    .. method:: get_context_data(**kwargs)
    342345
  • tests/regressiontests/generic_views/list.py

     
    1919        self.assertEqual(res.status_code, 200)
    2020        self.assertTemplateUsed(res, 'generic_views/author_list.html')
    2121        self.assertEqual(list(res.context['object_list']), list(Author.objects.all()))
    22         self.assertIs(res.context['authors'], res.context['object_list'])
     22        self.assertIs(res.context['author_list'], res.context['object_list'])
    2323        self.assertIsNone(res.context['paginator'])
    2424        self.assertIsNone(res.context['page_obj'])
    2525        self.assertFalse(res.context['is_paginated'])
     
    3030        self.assertEqual(res.status_code, 200)
    3131        self.assertTemplateUsed(res, 'generic_views/author_list.html')
    3232        self.assertEqual(len(res.context['object_list']), 30)
    33         self.assertIs(res.context['authors'], res.context['object_list'])
     33        self.assertIs(res.context['author_list'], res.context['object_list'])
    3434        self.assertTrue(res.context['is_paginated'])
    3535        self.assertEqual(res.context['page_obj'].number, 1)
    3636        self.assertEqual(res.context['paginator'].num_pages, 4)
    37         self.assertEqual(res.context['authors'][0].name, 'Author 00')
    38         self.assertEqual(list(res.context['authors'])[-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')
    3939
    4040    def test_paginated_queryset_shortdata(self):
    4141        # Test that short datasets ALSO result in a paginated view.
     
    4343        self.assertEqual(res.status_code, 200)
    4444        self.assertTemplateUsed(res, 'generic_views/author_list.html')
    4545        self.assertEqual(list(res.context['object_list']), list(Author.objects.all()))
    46         self.assertIs(res.context['authors'], res.context['object_list'])
     46        self.assertIs(res.context['author_list'], res.context['object_list'])
    4747        self.assertEqual(res.context['page_obj'].number, 1)
    4848        self.assertEqual(res.context['paginator'].num_pages, 1)
    4949        self.assertTrue(res.context['is_paginated'])
     
    5454        self.assertEqual(res.status_code, 200)
    5555        self.assertTemplateUsed(res, 'generic_views/author_list.html')
    5656        self.assertEqual(len(res.context['object_list']), 30)
    57         self.assertIs(res.context['authors'], res.context['object_list'])
    58         self.assertEqual(res.context['authors'][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')
    5959        self.assertEqual(res.context['page_obj'].number, 2)
    6060
    6161    def test_paginated_get_last_page_by_query_string(self):
     
    6363        res = self.client.get('/list/authors/paginated/', {'page': 'last'})
    6464        self.assertEqual(res.status_code, 200)
    6565        self.assertEqual(len(res.context['object_list']), 10)
    66         self.assertIs(res.context['authors'], res.context['object_list'])
    67         self.assertEqual(res.context['authors'][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')
    6868        self.assertEqual(res.context['page_obj'].number, 4)
    6969
    7070    def test_paginated_get_page_by_urlvar(self):
     
    7373        self.assertEqual(res.status_code, 200)
    7474        self.assertTemplateUsed(res, 'generic_views/author_list.html')
    7575        self.assertEqual(len(res.context['object_list']), 30)
    76         self.assertIs(res.context['authors'], res.context['object_list'])
    77         self.assertEqual(res.context['authors'][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')
    7878        self.assertEqual(res.context['page_obj'].number, 3)
    7979
    8080    def test_paginated_page_out_of_range(self):
     
    112112        self.assertEqual(res.status_code, 200)
    113113        self.assertTemplateUsed(res, 'generic_views/list.html')
    114114        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'])
    116116        self.assertIsNone(res.context['paginator'])
    117117        self.assertIsNone(res.context['page_obj'])
    118118        self.assertFalse(res.context['is_paginated'])
     
    128128        res = self.client.get('/list/authors/template_name/')
    129129        self.assertEqual(res.status_code, 200)
    130130        self.assertEqual(list(res.context['object_list']), list(Author.objects.all()))
    131         self.assertIs(res.context['authors'], res.context['object_list'])
     131        self.assertIs(res.context['author_list'], res.context['object_list'])
    132132        self.assertTemplateUsed(res, 'generic_views/list.html')
    133133
    134134    def test_template_name_suffix(self):
    135135        res = self.client.get('/list/authors/template_name_suffix/')
    136136        self.assertEqual(res.status_code, 200)
    137137        self.assertEqual(list(res.context['object_list']), list(Author.objects.all()))
    138         self.assertIs(res.context['authors'], res.context['object_list'])
     138        self.assertIs(res.context['author_list'], res.context['object_list'])
    139139        self.assertTemplateUsed(res, 'generic_views/author_objects.html')
    140140
    141141    def test_context_object_name(self):
  • tests/regressiontests/generic_views/dates.py

     
    9292        res = self.client.get('/dates/books/2006/make_object_list/')
    9393        self.assertEqual(res.status_code, 200)
    9494        self.assertEqual(list(res.context['date_list']), [datetime.datetime(2006, 5, 1)])
    95         self.assertEqual(list(res.context['books']), list(Book.objects.filter(pubdate__year=2006)))
     95        self.assertEqual(list(res.context['book_list']), list(Book.objects.filter(pubdate__year=2006)))
    9696        self.assertEqual(list(res.context['object_list']), list(Book.objects.filter(pubdate__year=2006)))
    9797        self.assertTemplateUsed(res, 'generic_views/book_archive_year.html')
    9898
     
    102102        res = self.client.get('/dates/books/1999/allow_empty/')
    103103        self.assertEqual(res.status_code, 200)
    104104        self.assertEqual(list(res.context['date_list']), [])
    105         self.assertEqual(list(res.context['books']), [])
     105        self.assertEqual(list(res.context['book_list']), [])
    106106
    107107    def test_year_view_allow_future(self):
    108108        # Create a new book in the future
     
    113113
    114114        res = self.client.get('/dates/books/%s/allow_empty/' % year)
    115115        self.assertEqual(res.status_code, 200)
    116         self.assertEqual(list(res.context['books']), [])
     116        self.assertEqual(list(res.context['book_list']), [])
    117117
    118118        res = self.client.get('/dates/books/%s/allow_future/' % year)
    119119        self.assertEqual(res.status_code, 200)
     
    132132        self.assertEqual(res.status_code, 200)
    133133        self.assertTemplateUsed(res, 'generic_views/book_archive_month.html')
    134134        self.assertEqual(list(res.context['date_list']), [datetime.datetime(2008, 10, 1)])
    135         self.assertEqual(list(res.context['books']),
     135        self.assertEqual(list(res.context['book_list']),
    136136                         list(Book.objects.filter(pubdate=datetime.date(2008, 10, 1))))
    137137        self.assertEqual(res.context['month'], datetime.date(2008, 10, 1))
    138138
     
    149149        res = self.client.get('/dates/books/2000/jan/allow_empty/')
    150150        self.assertEqual(res.status_code, 200)
    151151        self.assertEqual(list(res.context['date_list']), [])
    152         self.assertEqual(list(res.context['books']), [])
     152        self.assertEqual(list(res.context['book_list']), [])
    153153        self.assertEqual(res.context['month'], datetime.date(2000, 1, 1))
    154154
    155155        # Since it's allow empty, next/prev are allowed to be empty months (#7164)
     
    175175        res = self.client.get('/dates/books/%s/allow_future/' % urlbit)
    176176        self.assertEqual(res.status_code, 200)
    177177        self.assertEqual(res.context['date_list'][0].date(), b.pubdate)
    178         self.assertEqual(list(res.context['books']), [b])
     178        self.assertEqual(list(res.context['book_list']), [b])
    179179        self.assertEqual(res.context['month'], future)
    180180
    181181        # Since it's allow_future but not allow_empty, next/prev are not
     
    229229        res = self.client.get('/dates/books/2008/week/39/')
    230230        self.assertEqual(res.status_code, 200)
    231231        self.assertTemplateUsed(res, 'generic_views/book_archive_week.html')
    232         self.assertEqual(res.context['books'][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)))
    233233        self.assertEqual(res.context['week'], datetime.date(2008, 9, 28))
    234234
    235235    def test_week_view_allow_empty(self):
     
    238238
    239239        res = self.client.get('/dates/books/2008/week/12/allow_empty/')
    240240        self.assertEqual(res.status_code, 200)
    241         self.assertEqual(list(res.context['books']), [])
     241        self.assertEqual(list(res.context['book_list']), [])
    242242
    243243    def test_week_view_allow_future(self):
    244244        future = datetime.date(datetime.date.today().year + 1, 1, 1)
     
    249249
    250250        res = self.client.get('/dates/books/%s/week/1/allow_future/' % future.year)
    251251        self.assertEqual(res.status_code, 200)
    252         self.assertEqual(list(res.context['books']), [b])
     252        self.assertEqual(list(res.context['book_list']), [b])
    253253
    254254    def test_week_view_invalid_pattern(self):
    255255        res = self.client.get('/dates/books/2007/week/no_week/')
     
    273273        res = self.client.get('/dates/books/2008/oct/01/')
    274274        self.assertEqual(res.status_code, 200)
    275275        self.assertTemplateUsed(res, 'generic_views/book_archive_day.html')
    276         self.assertEqual(list(res.context['books']),
     276        self.assertEqual(list(res.context['book_list']),
    277277                         list(Book.objects.filter(pubdate=datetime.date(2008, 10, 1))))
    278278        self.assertEqual(res.context['day'], datetime.date(2008, 10, 1))
    279279
     
    289289        # allow_empty = True, empty month
    290290        res = self.client.get('/dates/books/2000/jan/1/allow_empty/')
    291291        self.assertEqual(res.status_code, 200)
    292         self.assertEqual(list(res.context['books']), [])
     292        self.assertEqual(list(res.context['book_list']), [])
    293293        self.assertEqual(res.context['day'], datetime.date(2000, 1, 1))
    294294
    295295        # Since it's allow empty, next/prev are allowed to be empty months (#7164)
     
    314314        # allow_future = True, valid future month
    315315        res = self.client.get('/dates/books/%s/allow_future/' % urlbit)
    316316        self.assertEqual(res.status_code, 200)
    317         self.assertEqual(list(res.context['books']), [b])
     317        self.assertEqual(list(res.context['book_list']), [b])
    318318        self.assertEqual(res.context['day'], future)
    319319
    320320        # allow_future but not allow_empty, next/prev amust be valid
  • tests/regressiontests/generic_views/detail.py

     
    3232        res = self.client.get('/detail/artist/1/')
    3333        self.assertEqual(res.status_code, 200)
    3434        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))
    3636        self.assertTemplateUsed(res, 'generic_views/artist_detail.html')
    3737
    3838    def test_template_name(self):
  • django/views/generic/list.py

     
    7676        if self.context_object_name:
    7777            return self.context_object_name
    7878        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())
    8180        else:
    8281            return None
    8382
  • django/views/generic/detail.py

     
    8080        if self.context_object_name:
    8181            return self.context_object_name
    8282        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())
    8584        else:
    8685            return None
    8786
Back to Top