ListView is a class within the django.views.generic.list module of the Django project.
django-tables2 (projection documentation and PyPI page) is a code library for Django that simplifies creating and displaying tables in Django templates, especially with more advanced features such as pagination and sorting. The project and its code are available as open source.
django-tables2 / django_tables2 / views.py
# views.py
from itertools import count
from django.core.exceptions import ImproperlyConfigured
from django.views.generic.list import ListView
from . import tables
from .config import RequestConfig
class TableMixinBase:
context_table_name = "table"
table_pagination = None
def get_context_table_name(self, table):
return self.context_table_name
def get_table_pagination(self, table):
paginate = self.table_pagination
if paginate is False:
return False
paginate = {}
if getattr(self, "paginate_by", None) is not None:
paginate["per_page"] = self.paginate_by
if hasattr(self, "paginator_class"):
paginate["paginator_class"] = self.paginator_class
if getattr(self, "paginate_orphans", 0) != 0:
## ... source file abbreviated to get to ListView examples ...
)
def get_table_data(self):
if self.table_data is not None:
return self.table_data
elif hasattr(self, "object_list"):
return self.object_list
elif hasattr(self, "get_queryset"):
return self.get_queryset()
klass = type(self).__name__
raise ImproperlyConfigured(
"Table data was not specified. Define {}.table_data".format(klass)
)
def get_table_kwargs(self):
return {}
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
table = self.get_table(**self.get_table_kwargs())
context[self.get_context_table_name(table)] = table
return context
class SingleTableView(SingleTableMixin, ListView):
class MultiTableMixin(TableMixinBase):
tables = None
tables_data = None
table_prefix = "table_{}-"
context_table_name = "tables"
def get_tables(self):
if self.tables is None:
klass = type(self).__name__
raise ImproperlyConfigured("No tables were specified. Define {}.tables".format(klass))
data = self.get_tables_data()
if data is None:
return self.tables
if len(data) != len(self.tables):
klass = type(self).__name__
raise ImproperlyConfigured("len({}.tables_data) != len({}.tables)".format(klass, klass))
return list(Table(data[i]) for i, Table in enumerate(self.tables))
## ... source file continues with no further ListView examples...
django-taggit (PyPI page) provides a way to create, store, manage and use tags in a Django project. The code for django-taggit is open source and maintained by the collaborative developer community group Jazzband.
django-taggit / taggit / views.py
# views.py
from django.contrib.contenttypes.models import ContentType
from django.shortcuts import get_object_or_404
from django.views.generic.list import ListView
from taggit.models import Tag, TaggedItem
def tagged_object_list(request, slug, queryset, **kwargs):
if callable(queryset):
queryset = queryset()
kwargs["slug"] = slug
tag_list_view = type(
"TagListView",
(TagListMixin, ListView),
{"model": queryset.model, "queryset": queryset},
)
return tag_list_view.as_view()(request, **kwargs)
class TagListMixin:
tag_suffix = "_tag"
def dispatch(self, request, *args, **kwargs):
slug = kwargs.pop("slug")
self.tag = get_object_or_404(Tag, slug=slug)
return super().dispatch(request, *args, **kwargs)
## ... source file continues with no further ListView examples...