2012-06-11 08:34:00 +00:00
|
|
|
|
===================================
|
|
|
|
|
Using mixins with class-based views
|
|
|
|
|
===================================
|
|
|
|
|
|
|
|
|
|
.. caution::
|
|
|
|
|
|
|
|
|
|
This is an advanced topic. A working knowledge of :doc:`Django's
|
|
|
|
|
class-based views<index>` is advised before exploring these
|
|
|
|
|
techniques.
|
|
|
|
|
|
|
|
|
|
Django's built-in class-based views provide a lot of functionality,
|
|
|
|
|
but some of it you may want to use separately. For instance, you may
|
|
|
|
|
want to write a view that renders a template to make the HTTP
|
|
|
|
|
response, but you can't use
|
|
|
|
|
:class:`~django.views.generic.base.TemplateView`; perhaps you need to
|
2013-03-22 09:50:45 +00:00
|
|
|
|
render a template only on ``POST``, with ``GET`` doing something else
|
2012-06-11 08:34:00 +00:00
|
|
|
|
entirely. While you could use
|
|
|
|
|
:class:`~django.template.response.TemplateResponse` directly, this
|
|
|
|
|
will likely result in duplicate code.
|
|
|
|
|
|
|
|
|
|
For this reason, Django also provides a number of mixins that provide
|
|
|
|
|
more discrete functionality. Template rendering, for instance, is
|
|
|
|
|
encapsulated in the
|
|
|
|
|
:class:`~django.views.generic.base.TemplateResponseMixin`. The Django
|
|
|
|
|
reference documentation contains :doc:`full documentation of all the
|
|
|
|
|
mixins</ref/class-based-views/mixins>`.
|
|
|
|
|
|
|
|
|
|
Context and template responses
|
|
|
|
|
==============================
|
|
|
|
|
|
|
|
|
|
Two central mixins are provided that help in providing a consistent
|
|
|
|
|
interface to working with templates in class-based views.
|
|
|
|
|
|
|
|
|
|
:class:`~django.views.generic.base.TemplateResponseMixin`
|
|
|
|
|
Every built in view which returns a
|
|
|
|
|
:class:`~django.template.response.TemplateResponse` will call the
|
2013-06-26 11:25:24 +00:00
|
|
|
|
:meth:`~django.views.generic.base.TemplateResponseMixin.render_to_response()`
|
2013-01-01 13:12:42 +00:00
|
|
|
|
method that ``TemplateResponseMixin`` provides. Most of the time this
|
2012-06-11 08:34:00 +00:00
|
|
|
|
will be called for you (for instance, it is called by the ``get()`` method
|
|
|
|
|
implemented by both :class:`~django.views.generic.base.TemplateView` and
|
2013-01-01 13:12:42 +00:00
|
|
|
|
:class:`~django.views.generic.detail.DetailView`); similarly, it's unlikely
|
2012-06-11 08:34:00 +00:00
|
|
|
|
that you'll need to override it, although if you want your response to
|
|
|
|
|
return something not rendered via a Django template then you'll want to do
|
|
|
|
|
it. For an example of this, see the :ref:`JSONResponseMixin example
|
|
|
|
|
<jsonresponsemixin-example>`.
|
|
|
|
|
|
2013-06-26 11:25:24 +00:00
|
|
|
|
``render_to_response()`` itself calls
|
2012-06-11 08:34:00 +00:00
|
|
|
|
:meth:`~django.views.generic.base.TemplateResponseMixin.get_template_names`,
|
2019-06-17 14:54:55 +00:00
|
|
|
|
which by default will look up
|
2012-06-11 08:34:00 +00:00
|
|
|
|
:attr:`~django.views.generic.base.TemplateResponseMixin.template_name` on
|
|
|
|
|
the class-based view; two other mixins
|
|
|
|
|
(:class:`~django.views.generic.detail.SingleObjectTemplateResponseMixin`
|
|
|
|
|
and
|
|
|
|
|
:class:`~django.views.generic.list.MultipleObjectTemplateResponseMixin`)
|
|
|
|
|
override this to provide more flexible defaults when dealing with actual
|
|
|
|
|
objects.
|
|
|
|
|
|
|
|
|
|
:class:`~django.views.generic.base.ContextMixin`
|
|
|
|
|
Every built in view which needs context data, such as for rendering a
|
2013-01-01 13:12:42 +00:00
|
|
|
|
template (including ``TemplateResponseMixin`` above), should call
|
2013-06-26 11:25:24 +00:00
|
|
|
|
:meth:`~django.views.generic.base.ContextMixin.get_context_data()` passing
|
2012-06-11 08:34:00 +00:00
|
|
|
|
any data they want to ensure is in there as keyword arguments.
|
2013-06-26 11:25:24 +00:00
|
|
|
|
``get_context_data()`` returns a dictionary; in ``ContextMixin`` it
|
2019-06-17 14:54:55 +00:00
|
|
|
|
returns its keyword arguments, but it is common to override this to add
|
|
|
|
|
more members to the dictionary. You can also use the
|
2017-07-06 14:34:54 +00:00
|
|
|
|
:attr:`~django.views.generic.base.ContextMixin.extra_context` attribute.
|
2012-06-11 08:34:00 +00:00
|
|
|
|
|
|
|
|
|
Building up Django's generic class-based views
|
2012-08-25 12:11:56 +00:00
|
|
|
|
==============================================
|
2012-06-11 08:34:00 +00:00
|
|
|
|
|
|
|
|
|
Let's look at how two of Django's generic class-based views are built
|
|
|
|
|
out of mixins providing discrete functionality. We'll consider
|
|
|
|
|
:class:`~django.views.generic.detail.DetailView`, which renders a
|
|
|
|
|
"detail" view of an object, and
|
|
|
|
|
:class:`~django.views.generic.list.ListView`, which will render a list
|
|
|
|
|
of objects, typically from a queryset, and optionally paginate
|
|
|
|
|
them. This will introduce us to four mixins which between them provide
|
|
|
|
|
useful functionality when working with either a single Django object,
|
|
|
|
|
or multiple objects.
|
|
|
|
|
|
|
|
|
|
There are also mixins involved in the generic edit views
|
|
|
|
|
(:class:`~django.views.generic.edit.FormView`, and the model-specific
|
|
|
|
|
views :class:`~django.views.generic.edit.CreateView`,
|
|
|
|
|
:class:`~django.views.generic.edit.UpdateView` and
|
|
|
|
|
:class:`~django.views.generic.edit.DeleteView`), and in the
|
|
|
|
|
date-based generic views. These are
|
|
|
|
|
covered in the :doc:`mixin reference
|
|
|
|
|
documentation</ref/class-based-views/mixins>`.
|
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
|
``DetailView``: working with a single Django object
|
|
|
|
|
---------------------------------------------------
|
2012-06-11 08:34:00 +00:00
|
|
|
|
|
|
|
|
|
To show the detail of an object, we basically need to do two things:
|
|
|
|
|
we need to look up the object and then we need to make a
|
2012-12-29 15:35:12 +00:00
|
|
|
|
:class:`~django.template.response.TemplateResponse` with a suitable template,
|
|
|
|
|
and that object as context.
|
2012-06-11 08:34:00 +00:00
|
|
|
|
|
|
|
|
|
To get the object, :class:`~django.views.generic.detail.DetailView`
|
|
|
|
|
relies on :class:`~django.views.generic.detail.SingleObjectMixin`,
|
|
|
|
|
which provides a
|
|
|
|
|
:meth:`~django.views.generic.detail.SingleObjectMixin.get_object`
|
|
|
|
|
method that figures out the object based on the URL of the request (it
|
|
|
|
|
looks for ``pk`` and ``slug`` keyword arguments as declared in the
|
|
|
|
|
URLConf, and looks the object up either from the
|
|
|
|
|
:attr:`~django.views.generic.detail.SingleObjectMixin.model` attribute
|
|
|
|
|
on the view, or the
|
|
|
|
|
:attr:`~django.views.generic.detail.SingleObjectMixin.queryset`
|
2013-01-01 13:12:42 +00:00
|
|
|
|
attribute if that's provided). ``SingleObjectMixin`` also overrides
|
2013-06-26 11:25:24 +00:00
|
|
|
|
:meth:`~django.views.generic.base.ContextMixin.get_context_data()`,
|
2012-06-11 08:34:00 +00:00
|
|
|
|
which is used across all Django's built in class-based views to supply
|
|
|
|
|
context data for template renders.
|
|
|
|
|
|
2012-12-29 15:35:12 +00:00
|
|
|
|
To then make a :class:`~django.template.response.TemplateResponse`,
|
|
|
|
|
:class:`DetailView` uses
|
2012-06-11 08:34:00 +00:00
|
|
|
|
:class:`~django.views.generic.detail.SingleObjectTemplateResponseMixin`,
|
2012-12-29 15:35:12 +00:00
|
|
|
|
which extends :class:`~django.views.generic.base.TemplateResponseMixin`,
|
2013-01-01 13:12:42 +00:00
|
|
|
|
overriding
|
|
|
|
|
:meth:`~django.views.generic.base.TemplateResponseMixin.get_template_names()`
|
|
|
|
|
as discussed above. It actually provides a fairly sophisticated set of options,
|
|
|
|
|
but the main one that most people are going to use is
|
2014-02-22 16:54:32 +00:00
|
|
|
|
``<app_label>/<model_name>_detail.html``. The ``_detail`` part can be changed
|
2013-01-01 13:12:42 +00:00
|
|
|
|
by setting
|
2012-06-11 08:34:00 +00:00
|
|
|
|
:attr:`~django.views.generic.detail.SingleObjectTemplateResponseMixin.template_name_suffix`
|
|
|
|
|
on a subclass to something else. (For instance, the :doc:`generic edit
|
|
|
|
|
views<generic-editing>` use ``_form`` for create and update views, and
|
|
|
|
|
``_confirm_delete`` for delete views.)
|
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
|
``ListView``: working with many Django objects
|
|
|
|
|
----------------------------------------------
|
2012-06-11 08:34:00 +00:00
|
|
|
|
|
|
|
|
|
Lists of objects follow roughly the same pattern: we need a (possibly
|
2013-01-01 13:12:42 +00:00
|
|
|
|
paginated) list of objects, typically a
|
|
|
|
|
:class:`~django.db.models.query.QuerySet`, and then we need to make a
|
|
|
|
|
:class:`~django.template.response.TemplateResponse` with a suitable template
|
|
|
|
|
using that list of objects.
|
2012-06-11 08:34:00 +00:00
|
|
|
|
|
|
|
|
|
To get the objects, :class:`~django.views.generic.list.ListView` uses
|
|
|
|
|
:class:`~django.views.generic.list.MultipleObjectMixin`, which
|
|
|
|
|
provides both
|
|
|
|
|
:meth:`~django.views.generic.list.MultipleObjectMixin.get_queryset`
|
|
|
|
|
and
|
|
|
|
|
:meth:`~django.views.generic.list.MultipleObjectMixin.paginate_queryset`. Unlike
|
2013-01-01 13:12:42 +00:00
|
|
|
|
with :class:`~django.views.generic.detail.SingleObjectMixin`, there's no need
|
|
|
|
|
to key off parts of the URL to figure out the queryset to work with, so the
|
2019-06-17 14:54:55 +00:00
|
|
|
|
default uses the
|
2012-06-11 08:34:00 +00:00
|
|
|
|
:attr:`~django.views.generic.list.MultipleObjectMixin.queryset` or
|
|
|
|
|
:attr:`~django.views.generic.list.MultipleObjectMixin.model` attribute
|
|
|
|
|
on the view class. A common reason to override
|
|
|
|
|
:meth:`~django.views.generic.list.MultipleObjectMixin.get_queryset`
|
|
|
|
|
here would be to dynamically vary the objects, such as depending on
|
|
|
|
|
the current user or to exclude posts in the future for a blog.
|
|
|
|
|
|
2013-01-01 13:12:42 +00:00
|
|
|
|
:class:`~django.views.generic.list.MultipleObjectMixin` also overrides
|
2013-06-26 11:25:24 +00:00
|
|
|
|
:meth:`~django.views.generic.base.ContextMixin.get_context_data()` to
|
2012-06-11 08:34:00 +00:00
|
|
|
|
include appropriate context variables for pagination (providing
|
|
|
|
|
dummies if pagination is disabled). It relies on ``object_list`` being
|
|
|
|
|
passed in as a keyword argument, which :class:`ListView` arranges for
|
|
|
|
|
it.
|
|
|
|
|
|
2013-01-01 13:12:42 +00:00
|
|
|
|
To make a :class:`~django.template.response.TemplateResponse`,
|
|
|
|
|
:class:`ListView` then uses
|
2012-06-11 08:34:00 +00:00
|
|
|
|
:class:`~django.views.generic.list.MultipleObjectTemplateResponseMixin`;
|
2013-01-01 13:12:42 +00:00
|
|
|
|
as with :class:`~django.views.generic.detail.SingleObjectTemplateResponseMixin`
|
|
|
|
|
above, this overrides ``get_template_names()`` to provide :meth:`a range of
|
|
|
|
|
options <django.views.generic.list.MultipleObjectTemplateResponseMixin>`,
|
2012-06-11 08:34:00 +00:00
|
|
|
|
with the most commonly-used being
|
2014-02-22 16:54:32 +00:00
|
|
|
|
``<app_label>/<model_name>_list.html``, with the ``_list`` part again
|
2012-06-11 08:34:00 +00:00
|
|
|
|
being taken from the
|
|
|
|
|
:attr:`~django.views.generic.list.MultipleObjectTemplateResponseMixin.template_name_suffix`
|
|
|
|
|
attribute. (The date based generic views use suffixes such as ``_archive``,
|
|
|
|
|
``_archive_year`` and so on to use different templates for the various
|
2013-07-28 01:45:25 +00:00
|
|
|
|
specialized date-based list views.)
|
2012-06-11 08:34:00 +00:00
|
|
|
|
|
|
|
|
|
Using Django's class-based view mixins
|
|
|
|
|
======================================
|
|
|
|
|
|
2020-05-01 12:37:21 +00:00
|
|
|
|
Now we've seen how Django's generic class-based views use the provided mixins,
|
|
|
|
|
let's look at other ways we can combine them. We're still going to be combining
|
|
|
|
|
them with either built-in class-based views, or other generic class-based
|
|
|
|
|
views, but there are a range of rarer problems you can solve than are provided
|
|
|
|
|
for by Django out of the box.
|
2012-06-11 08:34:00 +00:00
|
|
|
|
|
|
|
|
|
.. warning::
|
|
|
|
|
|
|
|
|
|
Not all mixins can be used together, and not all generic class
|
|
|
|
|
based views can be used with all other mixins. Here we present a
|
|
|
|
|
few examples that do work; if you want to bring together other
|
|
|
|
|
functionality then you'll have to consider interactions between
|
|
|
|
|
attributes and methods that overlap between the different classes
|
|
|
|
|
you're using, and how `method resolution order`_ will affect which
|
|
|
|
|
versions of the methods will be called in what order.
|
|
|
|
|
|
|
|
|
|
The reference documentation for Django's :doc:`class-based
|
|
|
|
|
views</ref/class-based-views/index>` and :doc:`class-based view
|
|
|
|
|
mixins</ref/class-based-views/mixins>` will help you in
|
|
|
|
|
understanding which attributes and methods are likely to cause
|
|
|
|
|
conflict between different classes and mixins.
|
|
|
|
|
|
|
|
|
|
If in doubt, it's often better to back off and base your work on
|
|
|
|
|
:class:`View` or :class:`TemplateView`, perhaps with
|
2013-01-01 13:12:42 +00:00
|
|
|
|
:class:`~django.views.generic.detail.SingleObjectMixin` and
|
|
|
|
|
:class:`~django.views.generic.list.MultipleObjectMixin`. Although you
|
|
|
|
|
will probably end up writing more code, it is more likely to be clearly
|
|
|
|
|
understandable to someone else coming to it later, and with fewer
|
|
|
|
|
interactions to worry about you will save yourself some thinking. (Of
|
|
|
|
|
course, you can always dip into Django's implementation of the generic
|
2015-08-18 20:01:36 +00:00
|
|
|
|
class-based views for inspiration on how to tackle problems.)
|
2012-06-11 08:34:00 +00:00
|
|
|
|
|
2014-12-19 15:25:57 +00:00
|
|
|
|
.. _method resolution order: https://www.python.org/download/releases/2.3/mro/
|
2012-06-11 08:34:00 +00:00
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
|
Using ``SingleObjectMixin`` with View
|
|
|
|
|
-------------------------------------
|
2012-06-11 08:34:00 +00:00
|
|
|
|
|
2019-06-17 14:54:55 +00:00
|
|
|
|
If we want to write a class-based view that responds only to ``POST``, we'll
|
|
|
|
|
subclass :class:`~django.views.generic.base.View` and write a ``post()`` method
|
|
|
|
|
in the subclass. However if we want our processing to work on a particular
|
|
|
|
|
object, identified from the URL, we'll want the functionality provided by
|
2012-06-11 08:34:00 +00:00
|
|
|
|
:class:`~django.views.generic.detail.SingleObjectMixin`.
|
|
|
|
|
|
2014-03-02 15:00:30 +00:00
|
|
|
|
We'll demonstrate this with the ``Author`` model we used in the
|
2012-08-25 12:11:56 +00:00
|
|
|
|
:doc:`generic class-based views introduction<generic-display>`.
|
2012-06-11 08:34:00 +00:00
|
|
|
|
|
2018-09-10 17:00:34 +00:00
|
|
|
|
.. code-block:: python
|
2022-05-31 05:40:54 +00:00
|
|
|
|
:caption: ``views.py``
|
2012-06-11 08:34:00 +00:00
|
|
|
|
|
|
|
|
|
from django.http import HttpResponseForbidden, HttpResponseRedirect
|
2015-12-30 15:51:16 +00:00
|
|
|
|
from django.urls import reverse
|
2016-01-09 11:40:08 +00:00
|
|
|
|
from django.views import View
|
2012-06-11 08:34:00 +00:00
|
|
|
|
from django.views.generic.detail import SingleObjectMixin
|
|
|
|
|
from books.models import Author
|
2012-08-25 12:11:56 +00:00
|
|
|
|
|
2023-02-28 19:53:28 +00:00
|
|
|
|
|
2021-03-01 21:54:22 +00:00
|
|
|
|
class RecordInterestView(SingleObjectMixin, View):
|
2012-06-11 08:34:00 +00:00
|
|
|
|
"""Records the current user's interest in an author."""
|
2023-02-28 19:53:28 +00:00
|
|
|
|
|
2012-06-11 08:34:00 +00:00
|
|
|
|
model = Author
|
2012-08-25 12:11:56 +00:00
|
|
|
|
|
2012-06-11 08:34:00 +00:00
|
|
|
|
def post(self, request, *args, **kwargs):
|
2016-04-02 11:18:26 +00:00
|
|
|
|
if not request.user.is_authenticated:
|
2012-06-11 08:34:00 +00:00
|
|
|
|
return HttpResponseForbidden()
|
|
|
|
|
|
|
|
|
|
# Look up the author we're interested in.
|
|
|
|
|
self.object = self.get_object()
|
|
|
|
|
# Actually record interest somehow here!
|
|
|
|
|
|
|
|
|
|
return HttpResponseRedirect(
|
|
|
|
|
reverse("author-detail", kwargs={"pk": self.object.pk})
|
2023-02-28 19:53:28 +00:00
|
|
|
|
)
|
2012-06-11 08:34:00 +00:00
|
|
|
|
|
|
|
|
|
In practice you'd probably want to record the interest in a key-value
|
|
|
|
|
store rather than in a relational database, so we've left that bit
|
|
|
|
|
out. The only bit of the view that needs to worry about using
|
2013-01-01 13:12:42 +00:00
|
|
|
|
:class:`~django.views.generic.detail.SingleObjectMixin` is where we want to
|
2019-06-17 14:54:55 +00:00
|
|
|
|
look up the author we're interested in, which it does with a call to
|
|
|
|
|
``self.get_object()``. Everything else is taken care of for us by the mixin.
|
2012-06-11 08:34:00 +00:00
|
|
|
|
|
2014-08-18 14:30:44 +00:00
|
|
|
|
We can hook this into our URLs easily enough:
|
|
|
|
|
|
2018-09-10 17:00:34 +00:00
|
|
|
|
.. code-block:: python
|
2022-05-31 05:40:54 +00:00
|
|
|
|
:caption: ``urls.py``
|
2012-06-11 08:34:00 +00:00
|
|
|
|
|
2016-10-20 17:29:04 +00:00
|
|
|
|
from django.urls import path
|
2021-03-01 21:54:22 +00:00
|
|
|
|
from books.views import RecordInterestView
|
2012-06-11 08:34:00 +00:00
|
|
|
|
|
2014-04-02 00:46:34 +00:00
|
|
|
|
urlpatterns = [
|
2012-06-11 08:34:00 +00:00
|
|
|
|
# ...
|
2021-03-01 21:54:22 +00:00
|
|
|
|
path(
|
|
|
|
|
"author/<int:pk>/interest/",
|
|
|
|
|
RecordInterestView.as_view(),
|
|
|
|
|
name="author-interest",
|
|
|
|
|
),
|
2014-04-02 00:46:34 +00:00
|
|
|
|
]
|
2012-06-11 08:34:00 +00:00
|
|
|
|
|
|
|
|
|
Note the ``pk`` named group, which
|
|
|
|
|
:meth:`~django.views.generic.detail.SingleObjectMixin.get_object` uses
|
2012-12-29 15:35:12 +00:00
|
|
|
|
to look up the ``Author`` instance. You could also use a slug, or
|
2013-01-01 13:12:42 +00:00
|
|
|
|
any of the other features of
|
|
|
|
|
:class:`~django.views.generic.detail.SingleObjectMixin`.
|
2012-06-11 08:34:00 +00:00
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
|
Using ``SingleObjectMixin`` with ``ListView``
|
|
|
|
|
---------------------------------------------
|
2012-06-11 08:34:00 +00:00
|
|
|
|
|
|
|
|
|
:class:`~django.views.generic.list.ListView` provides built-in
|
|
|
|
|
pagination, but you might want to paginate a list of objects that are
|
|
|
|
|
all linked (by a foreign key) to another object. In our publishing
|
|
|
|
|
example, you might want to paginate through all the books by a
|
|
|
|
|
particular publisher.
|
|
|
|
|
|
|
|
|
|
One way to do this is to combine :class:`ListView` with
|
2013-01-01 13:12:42 +00:00
|
|
|
|
:class:`~django.views.generic.detail.SingleObjectMixin`, so that the queryset
|
|
|
|
|
for the paginated list of books can hang off the publisher found as the single
|
2012-06-11 08:34:00 +00:00
|
|
|
|
object. In order to do this, we need to have two different querysets:
|
|
|
|
|
|
2013-06-26 11:25:24 +00:00
|
|
|
|
``Book`` queryset for use by :class:`~django.views.generic.list.ListView`
|
2013-07-01 12:05:49 +00:00
|
|
|
|
Since we have access to the ``Publisher`` whose books we want to list, we
|
2019-06-17 14:54:55 +00:00
|
|
|
|
override ``get_queryset()`` and use the ``Publisher``’s :ref:`reverse
|
|
|
|
|
foreign key manager<backwards-related-objects>`.
|
2013-07-01 12:05:49 +00:00
|
|
|
|
|
|
|
|
|
``Publisher`` queryset for use in :meth:`~django.views.generic.detail.SingleObjectMixin.get_object()`
|
|
|
|
|
We'll rely on the default implementation of ``get_object()`` to fetch the
|
|
|
|
|
correct ``Publisher`` object.
|
|
|
|
|
However, we need to explicitly pass a ``queryset`` argument because
|
|
|
|
|
otherwise the default implementation of ``get_object()`` would call
|
|
|
|
|
``get_queryset()`` which we have overridden to return ``Book`` objects
|
|
|
|
|
instead of ``Publisher`` ones.
|
2012-06-11 08:34:00 +00:00
|
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
2013-01-01 13:12:42 +00:00
|
|
|
|
We have to think carefully about ``get_context_data()``.
|
|
|
|
|
Since both :class:`~django.views.generic.detail.SingleObjectMixin` and
|
|
|
|
|
:class:`ListView` will
|
2012-06-11 08:34:00 +00:00
|
|
|
|
put things in the context data under the value of
|
2014-02-28 16:44:03 +00:00
|
|
|
|
``context_object_name`` if it's set, we'll instead explicitly
|
2013-06-22 21:05:22 +00:00
|
|
|
|
ensure the ``Publisher`` is in the context data. :class:`ListView`
|
2012-06-11 08:34:00 +00:00
|
|
|
|
will add in the suitable ``page_obj`` and ``paginator`` for us
|
|
|
|
|
providing we remember to call ``super()``.
|
|
|
|
|
|
2021-03-01 21:54:22 +00:00
|
|
|
|
Now we can write a new ``PublisherDetailView``::
|
2012-06-11 08:34:00 +00:00
|
|
|
|
|
|
|
|
|
from django.views.generic import ListView
|
|
|
|
|
from django.views.generic.detail import SingleObjectMixin
|
|
|
|
|
from books.models import Publisher
|
2012-08-25 12:11:56 +00:00
|
|
|
|
|
2023-02-28 19:53:28 +00:00
|
|
|
|
|
2021-03-01 21:54:22 +00:00
|
|
|
|
class PublisherDetailView(SingleObjectMixin, ListView):
|
2012-06-11 08:34:00 +00:00
|
|
|
|
paginate_by = 2
|
|
|
|
|
template_name = "books/publisher_detail.html"
|
2012-08-25 12:11:56 +00:00
|
|
|
|
|
2013-06-22 21:05:22 +00:00
|
|
|
|
def get(self, request, *args, **kwargs):
|
2013-07-01 12:05:49 +00:00
|
|
|
|
self.object = self.get_object(queryset=Publisher.objects.all())
|
2017-01-22 06:57:14 +00:00
|
|
|
|
return super().get(request, *args, **kwargs)
|
2013-06-22 21:05:22 +00:00
|
|
|
|
|
2012-06-11 08:34:00 +00:00
|
|
|
|
def get_context_data(self, **kwargs):
|
2017-01-22 06:57:14 +00:00
|
|
|
|
context = super().get_context_data(**kwargs)
|
2013-06-22 21:05:22 +00:00
|
|
|
|
context["publisher"] = self.object
|
|
|
|
|
return context
|
2012-08-25 12:11:56 +00:00
|
|
|
|
|
2012-06-11 08:34:00 +00:00
|
|
|
|
def get_queryset(self):
|
|
|
|
|
return self.object.book_set.all()
|
|
|
|
|
|
2013-06-22 21:05:22 +00:00
|
|
|
|
Notice how we set ``self.object`` within ``get()`` so we
|
|
|
|
|
can use it again later in ``get_context_data()`` and ``get_queryset()``.
|
|
|
|
|
If you don't set ``template_name``, the template will default to the normal
|
2012-06-11 08:34:00 +00:00
|
|
|
|
:class:`ListView` choice, which in this case would be
|
|
|
|
|
``"books/book_list.html"`` because it's a list of books;
|
2013-01-01 13:12:42 +00:00
|
|
|
|
:class:`ListView` knows nothing about
|
|
|
|
|
:class:`~django.views.generic.detail.SingleObjectMixin`, so it doesn't have
|
2013-06-22 21:05:22 +00:00
|
|
|
|
any clue this view is anything to do with a ``Publisher``.
|
2012-06-11 08:34:00 +00:00
|
|
|
|
|
|
|
|
|
The ``paginate_by`` is deliberately small in the example so you don't
|
|
|
|
|
have to create lots of books to see the pagination working! Here's the
|
2013-06-22 21:05:22 +00:00
|
|
|
|
template you'd want to use:
|
|
|
|
|
|
2013-07-28 18:09:29 +00:00
|
|
|
|
.. code-block:: html+django
|
2012-06-11 08:34:00 +00:00
|
|
|
|
|
|
|
|
|
{% extends "base.html" %}
|
2012-08-25 12:11:56 +00:00
|
|
|
|
|
2012-06-11 08:34:00 +00:00
|
|
|
|
{% block content %}
|
|
|
|
|
<h2>Publisher {{ publisher.name }}</h2>
|
2012-08-25 12:11:56 +00:00
|
|
|
|
|
2012-06-11 08:34:00 +00:00
|
|
|
|
<ol>
|
|
|
|
|
{% for book in page_obj %}
|
|
|
|
|
<li>{{ book.title }}</li>
|
|
|
|
|
{% endfor %}
|
|
|
|
|
</ol>
|
2012-08-25 12:11:56 +00:00
|
|
|
|
|
2012-06-11 08:34:00 +00:00
|
|
|
|
<div class="pagination">
|
|
|
|
|
<span class="step-links">
|
|
|
|
|
{% if page_obj.has_previous %}
|
|
|
|
|
<a href="?page={{ page_obj.previous_page_number }}">previous</a>
|
|
|
|
|
{% endif %}
|
2012-08-25 12:11:56 +00:00
|
|
|
|
|
2012-06-11 08:34:00 +00:00
|
|
|
|
<span class="current">
|
|
|
|
|
Page {{ page_obj.number }} of {{ paginator.num_pages }}.
|
|
|
|
|
</span>
|
2012-08-25 12:11:56 +00:00
|
|
|
|
|
2012-06-11 08:34:00 +00:00
|
|
|
|
{% if page_obj.has_next %}
|
|
|
|
|
<a href="?page={{ page_obj.next_page_number }}">next</a>
|
|
|
|
|
{% endif %}
|
|
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
{% endblock %}
|
|
|
|
|
|
|
|
|
|
Avoid anything more complex
|
|
|
|
|
===========================
|
|
|
|
|
|
|
|
|
|
Generally you can use
|
|
|
|
|
:class:`~django.views.generic.base.TemplateResponseMixin` and
|
|
|
|
|
:class:`~django.views.generic.detail.SingleObjectMixin` when you need
|
|
|
|
|
their functionality. As shown above, with a bit of care you can even
|
2013-01-01 13:12:42 +00:00
|
|
|
|
combine ``SingleObjectMixin`` with
|
2012-06-11 08:34:00 +00:00
|
|
|
|
:class:`~django.views.generic.list.ListView`. However things get
|
|
|
|
|
increasingly complex as you try to do so, and a good rule of thumb is:
|
|
|
|
|
|
|
|
|
|
.. hint::
|
|
|
|
|
|
|
|
|
|
Each of your views should use only mixins or views from one of the
|
|
|
|
|
groups of generic class-based views: :doc:`detail,
|
|
|
|
|
list<generic-display>`, :doc:`editing<generic-editing>` and
|
|
|
|
|
date. For example it's fine to combine
|
|
|
|
|
:class:`TemplateView` (built in view) with
|
2013-01-01 13:12:42 +00:00
|
|
|
|
:class:`~django.views.generic.list.MultipleObjectMixin` (generic list), but
|
|
|
|
|
you're likely to have problems combining ``SingleObjectMixin`` (generic
|
|
|
|
|
detail) with ``MultipleObjectMixin`` (generic list).
|
2012-06-11 08:34:00 +00:00
|
|
|
|
|
|
|
|
|
To show what happens when you try to get more sophisticated, we show
|
|
|
|
|
an example that sacrifices readability and maintainability when there
|
|
|
|
|
is a simpler solution. First, let's look at a naive attempt to combine
|
|
|
|
|
:class:`~django.views.generic.detail.DetailView` with
|
2016-04-19 00:25:54 +00:00
|
|
|
|
:class:`~django.views.generic.edit.FormMixin` to enable us to
|
2013-01-01 13:12:42 +00:00
|
|
|
|
``POST`` a Django :class:`~django.forms.Form` to the same URL as we're
|
|
|
|
|
displaying an object using :class:`DetailView`.
|
2012-06-11 08:34:00 +00:00
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
|
Using ``FormMixin`` with ``DetailView``
|
|
|
|
|
---------------------------------------
|
2012-06-11 08:34:00 +00:00
|
|
|
|
|
|
|
|
|
Think back to our earlier example of using :class:`View` and
|
2013-01-01 13:12:42 +00:00
|
|
|
|
:class:`~django.views.generic.detail.SingleObjectMixin` together. We were
|
|
|
|
|
recording a user's interest in a particular author; say now that we want to
|
|
|
|
|
let them leave a message saying why they like them. Again, let's assume we're
|
2012-06-11 08:34:00 +00:00
|
|
|
|
not going to store this in a relational database but instead in
|
|
|
|
|
something more esoteric that we won't worry about here.
|
|
|
|
|
|
2013-01-01 13:12:42 +00:00
|
|
|
|
At this point it's natural to reach for a :class:`~django.forms.Form` to
|
|
|
|
|
encapsulate the information sent from the user's browser to Django. Say also
|
|
|
|
|
that we're heavily invested in `REST`_, so we want to use the same URL for
|
2012-06-11 08:34:00 +00:00
|
|
|
|
displaying the author as for capturing the message from the
|
2012-12-29 15:35:12 +00:00
|
|
|
|
user. Let's rewrite our ``AuthorDetailView`` to do that.
|
2012-06-11 08:34:00 +00:00
|
|
|
|
|
2015-08-08 10:02:32 +00:00
|
|
|
|
.. _REST: https://en.wikipedia.org/wiki/Representational_state_transfer
|
2012-06-11 08:34:00 +00:00
|
|
|
|
|
|
|
|
|
We'll keep the ``GET`` handling from :class:`DetailView`, although
|
2013-01-01 13:12:42 +00:00
|
|
|
|
we'll have to add a :class:`~django.forms.Form` into the context data so we can
|
2012-06-11 08:34:00 +00:00
|
|
|
|
render it in the template. We'll also want to pull in form processing
|
|
|
|
|
from :class:`~django.views.generic.edit.FormMixin`, and write a bit of
|
|
|
|
|
code so that on ``POST`` the form gets called appropriately.
|
|
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
2013-01-01 13:12:42 +00:00
|
|
|
|
We use :class:`~django.views.generic.edit.FormMixin` and implement
|
|
|
|
|
``post()`` ourselves rather than try to mix :class:`DetailView` with
|
|
|
|
|
:class:`FormView` (which provides a suitable ``post()`` already) because
|
|
|
|
|
both of the views implement ``get()``, and things would get much more
|
2012-06-11 08:34:00 +00:00
|
|
|
|
confusing.
|
|
|
|
|
|
2021-03-01 21:54:22 +00:00
|
|
|
|
Our new ``AuthorDetailView`` looks like this::
|
2012-06-11 08:34:00 +00:00
|
|
|
|
|
|
|
|
|
# CAUTION: you almost certainly do not want to do this.
|
|
|
|
|
# It is provided as part of a discussion of problems you can
|
|
|
|
|
# run into when combining different generic class-based view
|
|
|
|
|
# functionality that is not designed to be used together.
|
|
|
|
|
|
|
|
|
|
from django import forms
|
|
|
|
|
from django.http import HttpResponseForbidden
|
2015-12-30 15:51:16 +00:00
|
|
|
|
from django.urls import reverse
|
2012-06-11 08:34:00 +00:00
|
|
|
|
from django.views.generic import DetailView
|
|
|
|
|
from django.views.generic.edit import FormMixin
|
2013-05-18 12:00:52 +00:00
|
|
|
|
from books.models import Author
|
2012-06-11 08:34:00 +00:00
|
|
|
|
|
2023-02-28 19:53:28 +00:00
|
|
|
|
|
2012-06-11 08:34:00 +00:00
|
|
|
|
class AuthorInterestForm(forms.Form):
|
|
|
|
|
message = forms.CharField()
|
|
|
|
|
|
2023-02-28 19:53:28 +00:00
|
|
|
|
|
2021-03-01 21:54:22 +00:00
|
|
|
|
class AuthorDetailView(FormMixin, DetailView):
|
2012-06-11 08:34:00 +00:00
|
|
|
|
model = Author
|
|
|
|
|
form_class = AuthorInterestForm
|
|
|
|
|
|
|
|
|
|
def get_success_url(self):
|
2013-06-22 21:05:22 +00:00
|
|
|
|
return reverse("author-detail", kwargs={"pk": self.object.pk})
|
2012-08-25 12:11:56 +00:00
|
|
|
|
|
2012-06-11 08:34:00 +00:00
|
|
|
|
def post(self, request, *args, **kwargs):
|
2016-04-02 11:18:26 +00:00
|
|
|
|
if not request.user.is_authenticated:
|
2013-06-22 21:05:22 +00:00
|
|
|
|
return HttpResponseForbidden()
|
|
|
|
|
self.object = self.get_object()
|
2014-10-14 18:56:39 +00:00
|
|
|
|
form = self.get_form()
|
2012-06-11 08:34:00 +00:00
|
|
|
|
if form.is_valid():
|
|
|
|
|
return self.form_valid(form)
|
|
|
|
|
else:
|
|
|
|
|
return self.form_invalid(form)
|
2012-08-25 12:11:56 +00:00
|
|
|
|
|
2012-06-11 08:34:00 +00:00
|
|
|
|
def form_valid(self, form):
|
2013-06-22 21:05:22 +00:00
|
|
|
|
# Here, we would record the user's interest using the message
|
|
|
|
|
# passed in form.cleaned_data['message']
|
2017-01-22 06:57:14 +00:00
|
|
|
|
return super().form_valid(form)
|
2012-08-25 12:11:56 +00:00
|
|
|
|
|
2019-12-04 07:59:24 +00:00
|
|
|
|
``get_success_url()`` provides somewhere to redirect to, which gets used
|
|
|
|
|
in the default implementation of ``form_valid()``. We have to provide our
|
|
|
|
|
own ``post()`` as noted earlier.
|
2012-06-11 08:34:00 +00:00
|
|
|
|
|
|
|
|
|
A better solution
|
|
|
|
|
-----------------
|
|
|
|
|
|
2019-06-17 14:54:55 +00:00
|
|
|
|
The number of subtle interactions between
|
2013-01-01 13:12:42 +00:00
|
|
|
|
:class:`~django.views.generic.edit.FormMixin` and :class:`DetailView` is
|
|
|
|
|
already testing our ability to manage things. It's unlikely you'd want to
|
|
|
|
|
write this kind of class yourself.
|
2012-06-11 08:34:00 +00:00
|
|
|
|
|
2019-06-17 14:54:55 +00:00
|
|
|
|
In this case, you could write the ``post()`` method yourself, keeping
|
|
|
|
|
:class:`DetailView` as the only generic functionality, although writing
|
|
|
|
|
:class:`~django.forms.Form` handling code involves a lot of duplication.
|
2012-06-11 08:34:00 +00:00
|
|
|
|
|
2019-06-17 14:54:55 +00:00
|
|
|
|
Alternatively, it would still be less work than the above approach to
|
2012-06-11 08:34:00 +00:00
|
|
|
|
have a separate view for processing the form, which could use
|
|
|
|
|
:class:`~django.views.generic.edit.FormView` distinct from
|
|
|
|
|
:class:`DetailView` without concerns.
|
|
|
|
|
|
|
|
|
|
An alternative better solution
|
|
|
|
|
------------------------------
|
|
|
|
|
|
|
|
|
|
What we're really trying to do here is to use two different class
|
|
|
|
|
based views from the same URL. So why not do just that? We have a very
|
|
|
|
|
clear division here: ``GET`` requests should get the
|
2013-01-01 13:12:42 +00:00
|
|
|
|
:class:`DetailView` (with the :class:`~django.forms.Form` added to the context
|
2012-06-11 08:34:00 +00:00
|
|
|
|
data), and ``POST`` requests should get the :class:`FormView`. Let's
|
|
|
|
|
set up those views first.
|
|
|
|
|
|
2021-03-01 21:54:22 +00:00
|
|
|
|
The ``AuthorDetailView`` view is almost the same as :ref:`when we
|
|
|
|
|
first introduced AuthorDetailView<generic-views-extra-work>`; we have to
|
2013-01-01 13:12:42 +00:00
|
|
|
|
write our own ``get_context_data()`` to make the
|
2012-12-29 15:35:12 +00:00
|
|
|
|
``AuthorInterestForm`` available to the template. We'll skip the
|
2014-08-18 14:30:44 +00:00
|
|
|
|
``get_object()`` override from before for clarity::
|
2012-06-11 08:34:00 +00:00
|
|
|
|
|
|
|
|
|
from django import forms
|
2018-05-12 17:37:42 +00:00
|
|
|
|
from django.views.generic import DetailView
|
2012-06-11 08:34:00 +00:00
|
|
|
|
from books.models import Author
|
2012-08-25 12:11:56 +00:00
|
|
|
|
|
2023-02-28 19:53:28 +00:00
|
|
|
|
|
2012-06-11 08:34:00 +00:00
|
|
|
|
class AuthorInterestForm(forms.Form):
|
|
|
|
|
message = forms.CharField()
|
2012-08-25 12:11:56 +00:00
|
|
|
|
|
2023-02-28 19:53:28 +00:00
|
|
|
|
|
2021-03-01 21:54:22 +00:00
|
|
|
|
class AuthorDetailView(DetailView):
|
2013-06-22 21:05:22 +00:00
|
|
|
|
model = Author
|
2012-08-25 12:11:56 +00:00
|
|
|
|
|
2012-06-11 08:34:00 +00:00
|
|
|
|
def get_context_data(self, **kwargs):
|
2017-01-22 06:57:14 +00:00
|
|
|
|
context = super().get_context_data(**kwargs)
|
2013-06-22 21:05:22 +00:00
|
|
|
|
context["form"] = AuthorInterestForm()
|
|
|
|
|
return context
|
2012-08-25 12:11:56 +00:00
|
|
|
|
|
2023-03-21 21:02:24 +00:00
|
|
|
|
Then the ``AuthorInterestFormView`` is a :class:`FormView`, but we have to
|
|
|
|
|
bring in :class:`~django.views.generic.detail.SingleObjectMixin` so we can find
|
|
|
|
|
the author we're talking about, and we have to remember to set
|
|
|
|
|
``template_name`` to ensure that form errors will render the same template as
|
|
|
|
|
``AuthorDetailView`` is using on ``GET``::
|
2012-06-11 08:34:00 +00:00
|
|
|
|
|
2013-05-18 12:00:52 +00:00
|
|
|
|
from django.http import HttpResponseForbidden
|
2018-05-12 17:37:42 +00:00
|
|
|
|
from django.urls import reverse
|
2012-06-11 08:34:00 +00:00
|
|
|
|
from django.views.generic import FormView
|
|
|
|
|
from django.views.generic.detail import SingleObjectMixin
|
2012-08-25 12:11:56 +00:00
|
|
|
|
|
2023-02-28 19:53:28 +00:00
|
|
|
|
|
2021-03-01 21:54:22 +00:00
|
|
|
|
class AuthorInterestFormView(SingleObjectMixin, FormView):
|
2012-06-11 08:34:00 +00:00
|
|
|
|
template_name = "books/author_detail.html"
|
|
|
|
|
form_class = AuthorInterestForm
|
|
|
|
|
model = Author
|
|
|
|
|
|
2013-06-22 21:05:22 +00:00
|
|
|
|
def post(self, request, *args, **kwargs):
|
2016-04-02 11:18:26 +00:00
|
|
|
|
if not request.user.is_authenticated:
|
2012-06-11 08:34:00 +00:00
|
|
|
|
return HttpResponseForbidden()
|
|
|
|
|
self.object = self.get_object()
|
2017-01-22 06:57:14 +00:00
|
|
|
|
return super().post(request, *args, **kwargs)
|
2013-06-22 21:05:22 +00:00
|
|
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
|
return reverse("author-detail", kwargs={"pk": self.object.pk})
|
2012-06-11 08:34:00 +00:00
|
|
|
|
|
2021-03-01 21:54:22 +00:00
|
|
|
|
Finally we bring this together in a new ``AuthorView`` view. We
|
2013-01-01 13:12:42 +00:00
|
|
|
|
already know that calling :meth:`~django.views.generic.base.View.as_view()` on
|
|
|
|
|
a class-based view gives us something that behaves exactly like a function
|
|
|
|
|
based view, so we can do that at the point we choose between the two subviews.
|
2012-06-11 08:34:00 +00:00
|
|
|
|
|
2020-05-01 12:37:21 +00:00
|
|
|
|
You can pass through keyword arguments to
|
2013-01-01 13:12:42 +00:00
|
|
|
|
:meth:`~django.views.generic.base.View.as_view()` in the same way you
|
2021-03-01 21:54:22 +00:00
|
|
|
|
would in your URLconf, such as if you wanted the ``AuthorInterestFormView``
|
|
|
|
|
behavior to also appear at another URL but using a different template::
|
2012-06-11 08:34:00 +00:00
|
|
|
|
|
2016-01-09 11:40:08 +00:00
|
|
|
|
from django.views import View
|
2012-08-25 12:11:56 +00:00
|
|
|
|
|
|
|
|
|
|
2021-03-01 21:54:22 +00:00
|
|
|
|
class AuthorView(View):
|
2012-06-11 08:34:00 +00:00
|
|
|
|
def get(self, request, *args, **kwargs):
|
2021-03-01 21:54:22 +00:00
|
|
|
|
view = AuthorDetailView.as_view()
|
2012-06-11 08:34:00 +00:00
|
|
|
|
return view(request, *args, **kwargs)
|
2012-08-25 12:11:56 +00:00
|
|
|
|
|
2012-06-11 08:34:00 +00:00
|
|
|
|
def post(self, request, *args, **kwargs):
|
2021-03-01 21:54:22 +00:00
|
|
|
|
view = AuthorInterestFormView.as_view()
|
2012-06-11 08:34:00 +00:00
|
|
|
|
return view(request, *args, **kwargs)
|
|
|
|
|
|
|
|
|
|
This approach can also be used with any other generic class-based
|
|
|
|
|
views or your own class-based views inheriting directly from
|
|
|
|
|
:class:`View` or :class:`TemplateView`, as it keeps the different
|
|
|
|
|
views as separate as possible.
|
2012-08-25 12:11:56 +00:00
|
|
|
|
|
|
|
|
|
.. _jsonresponsemixin-example:
|
|
|
|
|
|
|
|
|
|
More than just HTML
|
|
|
|
|
===================
|
|
|
|
|
|
2015-08-18 20:01:36 +00:00
|
|
|
|
Where class-based views shine is when you want to do the same thing many times.
|
2012-08-25 12:11:56 +00:00
|
|
|
|
Suppose you're writing an API, and every view should return JSON instead of
|
|
|
|
|
rendered HTML.
|
|
|
|
|
|
|
|
|
|
We can create a mixin class to use in all of our views, handling the
|
|
|
|
|
conversion to JSON once.
|
|
|
|
|
|
2019-06-17 14:54:55 +00:00
|
|
|
|
For example, a JSON mixin might look something like this::
|
2012-08-25 12:11:56 +00:00
|
|
|
|
|
2014-07-29 11:45:16 +00:00
|
|
|
|
from django.http import JsonResponse
|
2012-08-25 12:11:56 +00:00
|
|
|
|
|
2023-02-28 19:53:28 +00:00
|
|
|
|
|
2017-06-26 14:30:31 +00:00
|
|
|
|
class JSONResponseMixin:
|
2012-08-25 12:11:56 +00:00
|
|
|
|
"""
|
|
|
|
|
A mixin that can be used to render a JSON response.
|
|
|
|
|
"""
|
2023-02-28 19:53:28 +00:00
|
|
|
|
|
2013-06-24 09:55:43 +00:00
|
|
|
|
def render_to_json_response(self, context, **response_kwargs):
|
2012-08-25 12:11:56 +00:00
|
|
|
|
"""
|
|
|
|
|
Returns a JSON response, transforming 'context' to make the payload.
|
|
|
|
|
"""
|
2014-07-29 11:45:16 +00:00
|
|
|
|
return JsonResponse(self.get_data(context), **response_kwargs)
|
2012-08-25 12:11:56 +00:00
|
|
|
|
|
2014-07-29 11:45:16 +00:00
|
|
|
|
def get_data(self, context):
|
|
|
|
|
"""
|
|
|
|
|
Returns an object that will be serialized as JSON by json.dumps().
|
|
|
|
|
"""
|
2012-08-25 12:11:56 +00:00
|
|
|
|
# Note: This is *EXTREMELY* naive; in reality, you'll need
|
|
|
|
|
# to do much more complex handling to ensure that arbitrary
|
|
|
|
|
# objects -- such as Django model instances or querysets
|
|
|
|
|
# -- can be serialized as JSON.
|
2014-07-29 11:45:16 +00:00
|
|
|
|
return context
|
2012-08-25 12:11:56 +00:00
|
|
|
|
|
2013-06-24 09:55:43 +00:00
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
|
|
Check out the :doc:`/topics/serialization` documentation for more
|
|
|
|
|
information on how to correctly transform Django models and querysets into
|
|
|
|
|
JSON.
|
|
|
|
|
|
2013-06-26 11:25:24 +00:00
|
|
|
|
This mixin provides a ``render_to_json_response()`` method with the same signature
|
2013-06-24 09:55:43 +00:00
|
|
|
|
as :func:`~django.views.generic.base.TemplateResponseMixin.render_to_response()`.
|
2019-06-17 14:54:55 +00:00
|
|
|
|
To use it, we need to mix it into a ``TemplateView`` for example, and override
|
|
|
|
|
``render_to_response()`` to call ``render_to_json_response()`` instead::
|
2012-08-25 12:11:56 +00:00
|
|
|
|
|
|
|
|
|
from django.views.generic import TemplateView
|
|
|
|
|
|
2023-02-28 19:53:28 +00:00
|
|
|
|
|
2012-08-25 12:11:56 +00:00
|
|
|
|
class JSONView(JSONResponseMixin, TemplateView):
|
2013-06-24 09:55:43 +00:00
|
|
|
|
def render_to_response(self, context, **response_kwargs):
|
|
|
|
|
return self.render_to_json_response(context, **response_kwargs)
|
2012-08-25 12:11:56 +00:00
|
|
|
|
|
|
|
|
|
Equally we could use our mixin with one of the generic views. We can make our
|
|
|
|
|
own version of :class:`~django.views.generic.detail.DetailView` by mixing
|
2013-01-01 13:12:42 +00:00
|
|
|
|
``JSONResponseMixin`` with the
|
2021-02-03 14:25:56 +00:00
|
|
|
|
:class:`~django.views.generic.detail.BaseDetailView` -- (the
|
2012-08-25 12:11:56 +00:00
|
|
|
|
:class:`~django.views.generic.detail.DetailView` before template
|
|
|
|
|
rendering behavior has been mixed in)::
|
|
|
|
|
|
2013-05-18 12:00:52 +00:00
|
|
|
|
from django.views.generic.detail import BaseDetailView
|
|
|
|
|
|
2023-02-28 19:53:28 +00:00
|
|
|
|
|
2012-08-25 12:11:56 +00:00
|
|
|
|
class JSONDetailView(JSONResponseMixin, BaseDetailView):
|
2013-06-24 09:55:43 +00:00
|
|
|
|
def render_to_response(self, context, **response_kwargs):
|
|
|
|
|
return self.render_to_json_response(context, **response_kwargs)
|
2012-08-25 12:11:56 +00:00
|
|
|
|
|
|
|
|
|
This view can then be deployed in the same way as any other
|
|
|
|
|
:class:`~django.views.generic.detail.DetailView`, with exactly the
|
|
|
|
|
same behavior -- except for the format of the response.
|
|
|
|
|
|
|
|
|
|
If you want to be really adventurous, you could even mix a
|
|
|
|
|
:class:`~django.views.generic.detail.DetailView` subclass that is able
|
|
|
|
|
to return *both* HTML and JSON content, depending on some property of
|
2021-10-18 16:06:00 +00:00
|
|
|
|
the HTTP request, such as a query argument or an HTTP header. Mix in both the
|
2019-06-17 14:54:55 +00:00
|
|
|
|
``JSONResponseMixin`` and a
|
2012-08-25 12:11:56 +00:00
|
|
|
|
:class:`~django.views.generic.detail.SingleObjectTemplateResponseMixin`,
|
2013-01-01 13:12:42 +00:00
|
|
|
|
and override the implementation of
|
|
|
|
|
:func:`~django.views.generic.base.TemplateResponseMixin.render_to_response()`
|
2013-06-24 09:55:43 +00:00
|
|
|
|
to defer to the appropriate rendering method depending on the type of response
|
|
|
|
|
that the user requested::
|
2012-08-25 12:11:56 +00:00
|
|
|
|
|
2013-05-18 12:00:52 +00:00
|
|
|
|
from django.views.generic.detail import SingleObjectTemplateResponseMixin
|
|
|
|
|
|
2023-02-28 19:53:28 +00:00
|
|
|
|
|
2012-08-25 12:11:56 +00:00
|
|
|
|
class HybridDetailView(
|
|
|
|
|
JSONResponseMixin, SingleObjectTemplateResponseMixin, BaseDetailView
|
|
|
|
|
):
|
|
|
|
|
def render_to_response(self, context):
|
|
|
|
|
# Look for a 'format=json' GET argument
|
2013-06-24 09:55:43 +00:00
|
|
|
|
if self.request.GET.get("format") == "json":
|
|
|
|
|
return self.render_to_json_response(context)
|
2012-08-25 12:11:56 +00:00
|
|
|
|
else:
|
2017-01-22 06:57:14 +00:00
|
|
|
|
return super().render_to_response(context)
|
2012-08-25 12:11:56 +00:00
|
|
|
|
|
2013-06-24 09:55:43 +00:00
|
|
|
|
Because of the way that Python resolves method overloading, the call to
|
2017-01-22 06:57:14 +00:00
|
|
|
|
``super().render_to_response(context)`` ends up calling the
|
2013-06-26 11:25:24 +00:00
|
|
|
|
:meth:`~django.views.generic.base.TemplateResponseMixin.render_to_response()`
|
2013-06-24 09:55:43 +00:00
|
|
|
|
implementation of :class:`~django.views.generic.base.TemplateResponseMixin`.
|