2012-06-11 08:34:00 +00: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.
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``DetailView``
|
|
|
|
==============
|
2012-08-11 06:07:15 +00:00
|
|
|
|
2012-06-11 08:34:00 +00: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-08 17:46:08 +00:00
|
|
|
This view inherits methods and attributes from the following views:
|
|
|
|
|
2012-06-11 08:34:00 +00:00
|
|
|
* :class:`django.views.generic.detail.SingleObjectTemplateResponseMixin`
|
|
|
|
* :class:`django.views.generic.base.TemplateResponseMixin`
|
2013-01-01 13:12:42 +00:00
|
|
|
* ``django.views.generic.detail.BaseDetailView``
|
2012-06-11 08:34:00 +00:00
|
|
|
* :class:`django.views.generic.detail.SingleObjectMixin`
|
|
|
|
* :class:`django.views.generic.base.View`
|
|
|
|
|
|
|
|
**Method Flowchart**
|
|
|
|
|
2018-11-15 18:54:28 +00:00
|
|
|
#. :meth:`~django.views.generic.base.View.dispatch()`
|
|
|
|
#. :meth:`~django.views.generic.base.View.http_method_not_allowed()`
|
|
|
|
#. :meth:`~django.views.generic.base.TemplateResponseMixin.get_template_names()`
|
|
|
|
#. :meth:`~django.views.generic.detail.SingleObjectMixin.get_slug_field()`
|
|
|
|
#. :meth:`~django.views.generic.detail.SingleObjectMixin.get_queryset()`
|
|
|
|
#. :meth:`~django.views.generic.detail.SingleObjectMixin.get_object()`
|
|
|
|
#. :meth:`~django.views.generic.detail.SingleObjectMixin.get_context_object_name()`
|
|
|
|
#. :meth:`~django.views.generic.detail.SingleObjectMixin.get_context_data()`
|
|
|
|
#. ``get()``
|
|
|
|
#. :meth:`~django.views.generic.base.TemplateResponseMixin.render_to_response()`
|
2012-06-11 08:34:00 +00:00
|
|
|
|
2013-06-11 20:32:39 +00:00
|
|
|
**Example myapp/views.py**::
|
2012-06-11 08:34:00 +00:00
|
|
|
|
|
|
|
from django.utils import timezone
|
2018-05-12 17:37:42 +00:00
|
|
|
from django.views.generic.detail import DetailView
|
2012-06-11 08:34:00 +00:00
|
|
|
|
|
|
|
from articles.models import Article
|
|
|
|
|
|
|
|
class ArticleDetailView(DetailView):
|
2012-08-11 06:07:15 +00:00
|
|
|
|
2012-06-11 08:34:00 +00:00
|
|
|
model = Article
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
2017-01-22 06:57:14 +00:00
|
|
|
context = super().get_context_data(**kwargs)
|
2012-06-11 08:34:00 +00:00
|
|
|
context['now'] = timezone.now()
|
|
|
|
return context
|
|
|
|
|
2013-06-11 20:32:39 +00:00
|
|
|
**Example myapp/urls.py**::
|
2012-06-11 08:34:00 +00:00
|
|
|
|
2016-10-20 17:29:04 +00:00
|
|
|
from django.urls import path
|
2012-06-11 08:34:00 +00:00
|
|
|
|
|
|
|
from article.views import ArticleDetailView
|
|
|
|
|
2014-04-02 00:46:34 +00:00
|
|
|
urlpatterns = [
|
2017-09-25 17:06:11 +00:00
|
|
|
path('<slug:slug>/', ArticleDetailView.as_view(), name='article-detail'),
|
2014-04-02 00:46:34 +00:00
|
|
|
]
|
2012-08-11 06:07:15 +00:00
|
|
|
|
2013-06-11 20:32:39 +00:00
|
|
|
**Example myapp/article_detail.html**:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
|
|
|
|
|
|
|
<h1>{{ object.headline }}</h1>
|
|
|
|
<p>{{ object.content }}</p>
|
|
|
|
<p>Reporter: {{ object.reporter }}</p>
|
|
|
|
<p>Published: {{ object.pub_date|date }}</p>
|
2013-07-11 11:36:25 +00:00
|
|
|
<p>Date: {{ now|date }}</p>
|
2013-06-11 20:32:39 +00:00
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``ListView``
|
|
|
|
============
|
2012-06-11 08:34:00 +00: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-08 17:46:08 +00:00
|
|
|
**Ancestors (MRO)**
|
|
|
|
|
|
|
|
This view inherits methods and attributes from the following views:
|
2012-06-11 08:34:00 +00:00
|
|
|
|
|
|
|
* :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**
|
|
|
|
|
2018-11-15 18:54:28 +00:00
|
|
|
#. :meth:`~django.views.generic.base.View.dispatch()`
|
|
|
|
#. :meth:`~django.views.generic.base.View.http_method_not_allowed()`
|
|
|
|
#. :meth:`~django.views.generic.base.TemplateResponseMixin.get_template_names()`
|
|
|
|
#. :meth:`~django.views.generic.list.MultipleObjectMixin.get_queryset()`
|
|
|
|
#. :meth:`~django.views.generic.list.MultipleObjectMixin.get_context_object_name()`
|
|
|
|
#. :meth:`~django.views.generic.list.MultipleObjectMixin.get_context_data()`
|
|
|
|
#. ``get()``
|
|
|
|
#. :meth:`~django.views.generic.base.TemplateResponseMixin.render_to_response()`
|
2012-09-08 17:46:08 +00:00
|
|
|
|
|
|
|
**Example views.py**::
|
|
|
|
|
|
|
|
from django.utils import timezone
|
2018-05-12 17:37:42 +00:00
|
|
|
from django.views.generic.list import ListView
|
2012-09-08 17:46:08 +00:00
|
|
|
|
|
|
|
from articles.models import Article
|
|
|
|
|
|
|
|
class ArticleListView(ListView):
|
|
|
|
|
|
|
|
model = Article
|
2018-03-24 16:45:09 +00:00
|
|
|
paginate_by = 100 # if pagination is desired
|
2012-09-08 17:46:08 +00:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
2017-01-22 06:57:14 +00:00
|
|
|
context = super().get_context_data(**kwargs)
|
2012-09-08 17:46:08 +00:00
|
|
|
context['now'] = timezone.now()
|
|
|
|
return context
|
|
|
|
|
2013-06-11 20:32:39 +00:00
|
|
|
**Example myapp/urls.py**::
|
2012-09-08 17:46:08 +00:00
|
|
|
|
2016-10-20 17:29:04 +00:00
|
|
|
from django.urls import path
|
2012-09-08 17:46:08 +00:00
|
|
|
|
|
|
|
from article.views import ArticleListView
|
|
|
|
|
2014-04-02 00:46:34 +00:00
|
|
|
urlpatterns = [
|
2016-10-20 17:29:04 +00:00
|
|
|
path('', ArticleListView.as_view(), name='article-list'),
|
2014-04-02 00:46:34 +00:00
|
|
|
]
|
2012-09-08 17:46:08 +00:00
|
|
|
|
2013-06-11 20:32:39 +00:00
|
|
|
**Example myapp/article_list.html**:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
|
|
|
|
|
|
|
<h1>Articles</h1>
|
|
|
|
<ul>
|
|
|
|
{% for article in object_list %}
|
|
|
|
<li>{{ article.pub_date|date }} - {{ article.headline }}</li>
|
|
|
|
{% empty %}
|
|
|
|
<li>No articles yet.</li>
|
|
|
|
{% endfor %}
|
|
|
|
</ul>
|
|
|
|
|
2018-03-24 16:45:09 +00:00
|
|
|
If you're using pagination, you can adapt the :ref:`example template from
|
|
|
|
the pagination docs <using-paginator-in-view>`. Change instances of
|
|
|
|
``contacts`` in that example template to ``page_obj``.
|
|
|
|
|
2012-09-08 17:46:08 +00:00
|
|
|
.. 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 13:12:42 +00:00
|
|
|
Adds ``object_list`` to the context. If
|
2012-09-08 17:46:08 +00: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.
|