django/docs/ref/class-based-views/generic-display.txt

93 lines
2.7 KiB
Plaintext

=====================
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.object`` will contain the object that
the view is operating upon.
**Ancestors (MRO)**
* :class:`django.views.generic.detail.SingleObjectTemplateResponseMixin`
* :class:`django.views.generic.base.TemplateResponseMixin`
* :class:`django.views.generic.detail.BaseDetailView`
* :class:`django.views.generic.detail.SingleObjectMixin`
* :class:`django.views.generic.base.View`
**Method Flowchart**
1. :meth:`dispatch()`
2. :meth:`http_method_not_allowed()`
3. :meth:`get_template_names()`
4. :meth:`get_slug_field()`
5. :meth:`get_queryset()`
6. :meth:`get_object()`
7. :meth:`get_context_object_name()`
8. :meth:`get_context_data()`
9. :meth:`get()`
10. :meth:`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_list`` will contain the list of
objects (usually, but not necessarily a queryset) that the view is
operating upon.
**Mixins**
* :class:`django.views.generic.list.ListView`
* :class:`django.views.generic.list.MultipleObjectTemplateResponseMixin`
* :class:`django.views.generic.base.TemplateResponseMixin`
* :class:`django.views.generic.list.BaseListView`
* :class:`django.views.generic.list.MultipleObjectMixin`
* :class:`django.views.generic.base.View`
**Method Flowchart**
1. :meth:`dispatch()`
2. :meth:`http_method_not_allowed()`
3. :meth:`get_template_names()`
4. :meth:`get_queryset()`
5. :meth:`get_objects()`
6. :meth:`get_context_data()`
7. :meth:`get()`
8. :meth:`render_to_response()`