generic-display.txt 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. =====================
  2. Generic display views
  3. =====================
  4. The two following generic class-based views are designed to display data. On
  5. many projects they are typically the most commonly used views.
  6. ``DetailView``
  7. ==============
  8. .. class:: django.views.generic.detail.DetailView
  9. While this view is executing, ``self.object`` will contain the object that
  10. the view is operating upon.
  11. **Ancestors (MRO)**
  12. This view inherits methods and attributes from the following views:
  13. * :class:`django.views.generic.detail.SingleObjectTemplateResponseMixin`
  14. * :class:`django.views.generic.base.TemplateResponseMixin`
  15. * :class:`django.views.generic.detail.BaseDetailView`
  16. * :class:`django.views.generic.detail.SingleObjectMixin`
  17. * :class:`django.views.generic.base.View`
  18. **Method Flowchart**
  19. #. :meth:`~django.views.generic.base.View.setup()`
  20. #. :meth:`~django.views.generic.base.View.dispatch()`
  21. #. :meth:`~django.views.generic.base.View.http_method_not_allowed()`
  22. #. :meth:`~django.views.generic.base.TemplateResponseMixin.get_template_names()`
  23. #. :meth:`~django.views.generic.detail.SingleObjectMixin.get_slug_field()`
  24. #. :meth:`~django.views.generic.detail.SingleObjectMixin.get_queryset()`
  25. #. :meth:`~django.views.generic.detail.SingleObjectMixin.get_object()`
  26. #. :meth:`~django.views.generic.detail.SingleObjectMixin.get_context_object_name()`
  27. #. :meth:`~django.views.generic.detail.SingleObjectMixin.get_context_data()`
  28. #. :meth:`~django.views.generic.detail.BaseDetailView.get`
  29. #. :meth:`~django.views.generic.base.TemplateResponseMixin.render_to_response()`
  30. **Example myapp/views.py**::
  31. from django.utils import timezone
  32. from django.views.generic.detail import DetailView
  33. from articles.models import Article
  34. class ArticleDetailView(DetailView):
  35. model = Article
  36. def get_context_data(self, **kwargs):
  37. context = super().get_context_data(**kwargs)
  38. context["now"] = timezone.now()
  39. return context
  40. **Example myapp/urls.py**::
  41. from django.urls import path
  42. from article.views import ArticleDetailView
  43. urlpatterns = [
  44. path("<slug:slug>/", ArticleDetailView.as_view(), name="article-detail"),
  45. ]
  46. **Example myapp/article_detail.html**:
  47. .. code-block:: html+django
  48. <h1>{{ object.headline }}</h1>
  49. <p>{{ object.content }}</p>
  50. <p>Reporter: {{ object.reporter }}</p>
  51. <p>Published: {{ object.pub_date|date }}</p>
  52. <p>Date: {{ now|date }}</p>
  53. .. class:: django.views.generic.detail.BaseDetailView
  54. A base view for displaying a single object. It is not intended to be used
  55. directly, but rather as a parent class of the
  56. :class:`django.views.generic.detail.DetailView` or other views representing
  57. details of a single object.
  58. **Ancestors (MRO)**
  59. This view inherits methods and attributes from the following views:
  60. * :class:`django.views.generic.detail.SingleObjectMixin`
  61. * :class:`django.views.generic.base.View`
  62. **Methods**
  63. .. method:: get(request, *args, **kwargs)
  64. Adds ``object`` to the context.
  65. ``ListView``
  66. ============
  67. .. class:: django.views.generic.list.ListView
  68. A page representing a list of objects.
  69. While this view is executing, ``self.object_list`` will contain the list of
  70. objects (usually, but not necessarily a queryset) that the view is
  71. operating upon.
  72. **Ancestors (MRO)**
  73. This view inherits methods and attributes from the following views:
  74. * :class:`django.views.generic.list.MultipleObjectTemplateResponseMixin`
  75. * :class:`django.views.generic.base.TemplateResponseMixin`
  76. * :class:`django.views.generic.list.BaseListView`
  77. * :class:`django.views.generic.list.MultipleObjectMixin`
  78. * :class:`django.views.generic.base.View`
  79. **Method Flowchart**
  80. #. :meth:`~django.views.generic.base.View.setup()`
  81. #. :meth:`~django.views.generic.base.View.dispatch()`
  82. #. :meth:`~django.views.generic.base.View.http_method_not_allowed()`
  83. #. :meth:`~django.views.generic.base.TemplateResponseMixin.get_template_names()`
  84. #. :meth:`~django.views.generic.list.MultipleObjectMixin.get_queryset()`
  85. #. :meth:`~django.views.generic.list.MultipleObjectMixin.get_context_object_name()`
  86. #. :meth:`~django.views.generic.list.MultipleObjectMixin.get_context_data()`
  87. #. :meth:`~django.views.generic.list.BaseListView.get`
  88. #. :meth:`~django.views.generic.base.TemplateResponseMixin.render_to_response()`
  89. **Example views.py**::
  90. from django.utils import timezone
  91. from django.views.generic.list import ListView
  92. from articles.models import Article
  93. class ArticleListView(ListView):
  94. model = Article
  95. paginate_by = 100 # if pagination is desired
  96. def get_context_data(self, **kwargs):
  97. context = super().get_context_data(**kwargs)
  98. context["now"] = timezone.now()
  99. return context
  100. **Example myapp/urls.py**::
  101. from django.urls import path
  102. from article.views import ArticleListView
  103. urlpatterns = [
  104. path("", ArticleListView.as_view(), name="article-list"),
  105. ]
  106. **Example myapp/article_list.html**:
  107. .. code-block:: html+django
  108. <h1>Articles</h1>
  109. <ul>
  110. {% for article in object_list %}
  111. <li>{{ article.pub_date|date }} - {{ article.headline }}</li>
  112. {% empty %}
  113. <li>No articles yet.</li>
  114. {% endfor %}
  115. </ul>
  116. If you're using pagination, you can adapt the :ref:`example template from
  117. the pagination docs <paginating-a-list-view>`.
  118. .. class:: django.views.generic.list.BaseListView
  119. A base view for displaying a list of objects. It is not intended to be used
  120. directly, but rather as a parent class of the
  121. :class:`django.views.generic.list.ListView` or other views representing
  122. lists of objects.
  123. **Ancestors (MRO)**
  124. This view inherits methods and attributes from the following views:
  125. * :class:`django.views.generic.list.MultipleObjectMixin`
  126. * :class:`django.views.generic.base.View`
  127. **Methods**
  128. .. method:: get(request, *args, **kwargs)
  129. Adds ``object_list`` to the context. If
  130. :attr:`~django.views.generic.list.MultipleObjectMixin.allow_empty`
  131. is True then display an empty list. If
  132. :attr:`~django.views.generic.list.MultipleObjectMixin.allow_empty` is
  133. False then raise a 404 error.