Generic display views¶
The two following generic class-based views are designed to display data. On many projects they are typically the most commonly used views.
DetailView¶
-
class
django.views.generic.detail.DetailView¶ While this view is executing,
self.objectwill contain the object that the view is operating upon.Ancestors (MRO)
This view inherits methods and attributes from the following views:
django.views.generic.detail.SingleObjectTemplateResponseMixindjango.views.generic.base.TemplateResponseMixindjango.views.generic.detail.BaseDetailViewdjango.views.generic.detail.SingleObjectMixindjango.views.generic.base.View
Method Flowchart
dispatch()http_method_not_allowed()get_template_names()get_slug_field()get_queryset()get_object()get_context_object_name()get_context_data()get()render_to_response()
Example views.py:
from django.views.generic.detail import DetailView from django.utils import timezone from articles.models import Article class ArticleDetailView(DetailView): model = Article def get_context_data(self, **kwargs): context = super(ArticleDetailView, self).get_context_data(**kwargs) context['now'] = timezone.now() return context
Example urls.py:
from django.conf.urls import patterns, url from article.views import ArticleDetailView urlpatterns = patterns('', url(r'^(?P<slug>[-_\w]+)/$', ArticleDetailView.as_view(), name='article-detail'), )
ListView¶
-
class
django.views.generic.list.ListView¶ A page representing a list of objects.
While this view is executing,
self.object_listwill contain the list of objects (usually, but not necessarily a queryset) that the view is operating upon.Ancestors (MRO)
This view inherits methods and attributes from the following views:
django.views.generic.list.ListViewdjango.views.generic.list.MultipleObjectTemplateResponseMixindjango.views.generic.base.TemplateResponseMixindjango.views.generic.list.BaseListViewdjango.views.generic.list.MultipleObjectMixindjango.views.generic.base.View
Method Flowchart
dispatch()http_method_not_allowed()get_template_names()get_queryset()get_context_object_name()get_context_data()get()render_to_response()
Example views.py:
from django.views.generic.list import ListView from django.utils import timezone from articles.models import Article class ArticleListView(ListView): model = Article def get_context_data(self, **kwargs): context = super(ArticleListView, self).get_context_data(**kwargs) context['now'] = timezone.now() return context
Example urls.py:
from django.conf.urls import patterns, url from article.views import ArticleListView urlpatterns = patterns('', url(r'^$', ArticleListView.as_view(), name='article-list'), )
-
class
django.views.generic.list.BaseListView¶ A base view for displaying a list of objects. It is not intended to be used directly, but rather as a parent class of the
django.views.generic.list.ListViewor other views representing lists of objects.Ancestors (MRO)
This view inherits methods and attributes from the following views:
Methods
-
get(request, *args, **kwargs)¶ Adds
object_listto the context. Ifallow_emptyis True then display an empty list. Ifallow_emptyis False then raise a 404 error.
-