Skip to content
Browse files

Renamed items to queryset for ListView, for consistency.

  • Loading branch information...
1 parent 8a75082 commit 15b92a2cf4cb7bef67f87815e599164c0afd9881 @bfirsh committed Sep 7, 2010
Showing with 35 additions and 37 deletions.
  1. +31 −33 class_based_views/list.py
  2. +1 −1 class_based_views/tests/urls.py
  3. +3 −3 class_based_views/tests/views.py
View
64 class_based_views/list.py
@@ -6,49 +6,47 @@
class ListView(TemplateView):
"""
- Render some list of objects. This list may be any type via setting
- `self.items`, but if it's a queryset set on `self.queryset` then the
- queryset will be handled correctly.
+ Render some list of objects, set by `self.queryset`. This can be any
+ iterable of items, not just a queryset.
"""
allow_empty = True
template_object_name = None
queryset = None
- items = None
def GET(self, request, *args, **kwargs):
- items = self.get_items()
+ queryset = self.get_queryset()
allow_empty = self.get_allow_empty()
- if not allow_empty and len(items) == 0:
+ if not allow_empty and len(queryset) == 0:
raise Http404("Empty list and '%s.allow_empty' is False."
% self.__class__.__name__)
- context = self.get_context(items)
- return self.render_to_response(self.get_template_names(items), context)
+ context = self.get_context(queryset)
+ return self.render_to_response(self.get_template_names(queryset), context)
- def get_context(self, items):
+ def get_context(self, queryset):
"""
Get the context for this view.
"""
context = {
- 'object_list': items,
+ 'object_list': queryset,
}
- template_object_name = self.get_template_object_name(items)
+ template_object_name = self.get_template_object_name(queryset)
if template_object_name is not None:
- context[template_object_name] = items
+ context[template_object_name] = queryset
return context
- def get_items(self):
+ def get_queryset(self):
"""
Get the list of items for this view. This must be an interable, and may
be a queryset (in which qs-specific behavior will be enabled).
"""
- if hasattr(self, 'items') and self.items is not None:
- items = self.items
+ if hasattr(self, 'queryset') and self.queryset is not None:
+ queryset = self.queryset
else:
- raise ImproperlyConfigured("'%s' must define 'items'"
+ raise ImproperlyConfigured("'%s' must define 'queryset'"
% self.__class__.__name__)
- if hasattr(items, '_clone'):
- items = items._clone()
- return items
+ if hasattr(queryset, '_clone'):
+ queryset = queryset._clone()
+ return queryset
def get_allow_empty(self):
"""
@@ -57,7 +55,7 @@ def get_allow_empty(self):
"""
return self.allow_empty
- def get_template_names(self, items, suffix='list'):
+ def get_template_names(self, queryset, suffix='list'):
"""
Return a list of template names to be used for the request. Must return
a list. May not be called if get_template is overridden.
@@ -68,44 +66,44 @@ def get_template_names(self, items, suffix='list'):
# app and model name. This name gets put at the end of the template
# name list so that user-supplied names override the automatically-
# generated ones.
- if hasattr(items, 'model'):
- opts = items.model._meta
+ if hasattr(queryset, 'model'):
+ opts = queryset.model._meta
names.append("%s/%s_%s.html" % (opts.app_label, opts.object_name.lower(), suffix))
return names
- def get_template_object_name(self, items):
+ def get_template_object_name(self, queryset):
"""
Get the name of the item to be used in the context.
"""
if self.template_object_name:
return '%s_list' % self.template_object_name
- elif hasattr(items, 'model'):
- return smart_str(items.model._meta.verbose_name_plural)
+ elif hasattr(queryset, 'model'):
+ return smart_str(queryset.model._meta.verbose_name_plural)
else:
return None
class PaginatedListView(ListView):
paginate_by = None
- def get_context(self, items):
+ def get_context(self, queryset):
page = self.kwargs.get('page', None)
- paginator, page, items = self.paginate_items(items, page)
- context = super(PaginatedListView, self).get_context(items)
+ paginator, page, queryset = self.paginate_queryset(queryset, page)
+ context = super(PaginatedListView, self).get_context(queryset)
context.update({
'paginator': paginator,
'page_obj': page,
'is_paginated': paginator is not None,
})
return context
- def paginate_items(self, items, page):
+ def paginate_queryset(self, queryset, page):
"""
- Paginate the list of items, if needed.
+ Paginate the queryset, if needed.
"""
- paginate_by = self.get_paginate_by(items)
- paginator = Paginator(items, paginate_by, allow_empty_first_page=self.get_allow_empty())
+ paginate_by = self.get_paginate_by(queryset)
+ paginator = Paginator(queryset, paginate_by, allow_empty_first_page=self.get_allow_empty())
page = page or self.request.GET.get('page', 1)
try:
page_number = int(page)
@@ -120,7 +118,7 @@ def paginate_items(self, items, page):
except InvalidPage:
raise Http404('Invalid page (%s)' % page_number)
- def get_paginate_by(self, items):
+ def get_paginate_by(self, queryset):
"""
Get the number of items to paginate by, or ``None`` for no pagination.
"""
View
2 class_based_views/tests/urls.py
@@ -29,7 +29,7 @@
(r'^list/authors/paginated/(?P<page>\d+)/$', views.PaginatedAuthorList(paginate_by=30)),
(r'^list/authors/notempty/$', views.AuthorList(allow_empty=False)),
(r'^list/authors/template_object_name/$', views.AuthorList(template_object_name='author')),
- (r'^list/authors/invalid/$', views.AuthorList(items=None)),
+ (r'^list/authors/invalid/$', views.AuthorList(queryset=None)),
#
# # YearView
# # Mixing keyword and possitional captures below is intentional; the views
View
6 class_based_views/tests/views.py
@@ -24,20 +24,20 @@ class DecoratedAboutView(class_based_views.View):
class DictList(class_based_views.ListView):
"""A ListView that doesn't use a model."""
- items = [
+ queryset = [
{'first': 'John', 'last': 'Lennon'},
{'last': 'Yoko', 'last': 'Ono'}
]
template_name = 'tests/list.html'
class AuthorList(class_based_views.ListView):
- items = Author.objects.all()
+ queryset = Author.objects.all()
template_name = 'tests/list.html'
class PaginatedAuthorList(class_based_views.PaginatedListView):
- items = Author.objects.all()
+ queryset = Author.objects.all()
template_name = 'tests/list.html'

0 comments on commit 15b92a2

Please sign in to comment.
Something went wrong with that request. Please try again.