2012-06-11 16:34:00 +08:00
|
|
|
=====================
|
|
|
|
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.
|
|
|
|
|
2012-08-11 14:07:15 +08:00
|
|
|
DetailView
|
|
|
|
----------
|
|
|
|
|
2012-06-11 16:34:00 +08:00
|
|
|
.. 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)**
|
|
|
|
|
2012-09-09 01:46:08 +08:00
|
|
|
This view inherits methods and attributes from the following views:
|
|
|
|
|
2012-06-11 16:34:00 +08:00
|
|
|
* :class:`django.views.generic.detail.SingleObjectTemplateResponseMixin`
|
|
|
|
* :class:`django.views.generic.base.TemplateResponseMixin`
|
2013-01-01 21:12:42 +08:00
|
|
|
* ``django.views.generic.detail.BaseDetailView``
|
2012-06-11 16:34:00 +08:00
|
|
|
* :class:`django.views.generic.detail.SingleObjectMixin`
|
|
|
|
* :class:`django.views.generic.base.View`
|
|
|
|
|
|
|
|
**Method Flowchart**
|
|
|
|
|
2013-01-01 21:12:42 +08:00
|
|
|
1. :meth:`~django.views.generic.base.View.dispatch()`
|
|
|
|
2. :meth:`~django.views.generic.base.View.http_method_not_allowed()`
|
|
|
|
3. :meth:`~django.views.generic.base.TemplateResponseMixin.get_template_names()`
|
|
|
|
4. :meth:`~django.views.generic.detail.SingleObjectMixin.get_slug_field()`
|
|
|
|
5. :meth:`~django.views.generic.detail.SingleObjectMixin.get_queryset()`
|
|
|
|
6. :meth:`~django.views.generic.detail.SingleObjectMixin.get_object()`
|
|
|
|
7. :meth:`~django.views.generic.detail.SingleObjectMixin.get_context_object_name()`
|
|
|
|
8. :meth:`~django.views.generic.detail.SingleObjectMixin.get_context_data()`
|
|
|
|
9. ``get()``
|
|
|
|
10. :meth:`~django.views.generic.base.TemplateResponseMixin.render_to_response()`
|
2012-06-11 16:34:00 +08:00
|
|
|
|
|
|
|
**Example views.py**::
|
|
|
|
|
|
|
|
from django.views.generic.detail import DetailView
|
|
|
|
from django.utils import timezone
|
|
|
|
|
|
|
|
from articles.models import Article
|
|
|
|
|
|
|
|
class ArticleDetailView(DetailView):
|
2012-08-11 14:07:15 +08:00
|
|
|
|
2012-06-11 16:34:00 +08:00
|
|
|
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('',
|
2012-07-25 16:44:43 +08:00
|
|
|
url(r'^(?P<slug>[-_\w]+)/$', ArticleDetailView.as_view(), name='article-detail'),
|
2012-08-11 14:07:15 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
ListView
|
|
|
|
--------
|
2012-06-11 16:34:00 +08:00
|
|
|
|
|
|
|
.. 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.
|
|
|
|
|
2012-09-09 01:46:08 +08:00
|
|
|
**Ancestors (MRO)**
|
|
|
|
|
|
|
|
This view inherits methods and attributes from the following views:
|
2012-06-11 16:34:00 +08:00
|
|
|
|
|
|
|
* :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**
|
|
|
|
|
2013-01-01 21:12:42 +08:00
|
|
|
1. :meth:`~django.views.generic.base.View.dispatch()`
|
|
|
|
2. :meth:`~django.views.generic.base.View.http_method_not_allowed()`
|
|
|
|
3. :meth:`~django.views.generic.base.TemplateResponseMixin.get_template_names()`
|
|
|
|
4. :meth:`~django.views.generic.list.MultipleObjectMixin.get_queryset()`
|
|
|
|
5. :meth:`~django.views.generic.list.MultipleObjectMixin.get_context_object_name()`
|
|
|
|
6. :meth:`~django.views.generic.list.MultipleObjectMixin.get_context_data()`
|
|
|
|
7. ``get()``
|
|
|
|
8. :meth:`~django.views.generic.base.TemplateResponseMixin.render_to_response()`
|
2012-09-09 01:46:08 +08:00
|
|
|
|
|
|
|
|
|
|
|
**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
|
|
|
|
:class:`django.views.generic.list.ListView` or other views representing
|
|
|
|
lists of objects.
|
|
|
|
|
|
|
|
**Ancestors (MRO)**
|
|
|
|
|
|
|
|
This view inherits methods and attributes from the following views:
|
|
|
|
|
|
|
|
* :class:`django.views.generic.list.MultipleObjectMixin`
|
|
|
|
* :class:`django.views.generic.base.View`
|
|
|
|
|
|
|
|
**Methods**
|
|
|
|
|
|
|
|
.. method:: get(request, *args, **kwargs)
|
|
|
|
|
2013-01-01 21:12:42 +08:00
|
|
|
Adds ``object_list`` to the context. If
|
2012-09-09 01:46:08 +08:00
|
|
|
:attr:`~django.views.generic.list.MultipleObjectMixin.allow_empty`
|
|
|
|
is True then display an empty list. If
|
|
|
|
:attr:`~django.views.generic.list.MultipleObjectMixin.allow_empty` is
|
|
|
|
False then raise a 404 error.
|