{"title":"Using mixins with class-based views","version":"1.9","locale":"en","docname":"topics/class-based-views/mixins","url":"/en/1.9/topics/class-based-views/mixins/","canonical":"https://djangodocs.dev/en/1.9/topics/class-based-views/mixins/","summary":"Caution This is an advanced topic. A working knowledge of Django’s class-based views is advised before exploring these techniques. Django’s built-in class-based…","html":"<h1>Using mixins with class-based views<a class=\"heading-anchor\" href=\"#using-mixins-with-class-based-views\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<aside class=\"admonition admonition-caution\" role=\"note\">\n<p class=\"admonition-title\">Caution</p>\n<p>This is an advanced topic. A working knowledge of <a class=\"reference internal\" href=\"/en/1.9/topics/class-based-views/\"><span class=\"doc\">Django’s\nclass-based views</span></a> is advised before exploring these\ntechniques.</p>\n</aside>\n<p>Django’s built-in class-based views provide a lot of functionality,\nbut some of it you may want to use separately. For instance, you may\nwant to write a view that renders a template to make the HTTP\nresponse, but you can’t use\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/base/#django.views.generic.base.TemplateView\" title=\"django.views.generic.base.TemplateView\"><code class=\"xref py py-class docutils literal notranslate\">TemplateView</code></a>; perhaps you need to\nrender a template only on <code class=\"docutils literal notranslate\">POST</code>, with <code class=\"docutils literal notranslate\">GET</code> doing something else\nentirely. While you could use\n<a class=\"reference internal\" href=\"/en/1.9/ref/template-response/#django.template.response.TemplateResponse\" title=\"django.template.response.TemplateResponse\"><code class=\"xref py py-class docutils literal notranslate\">TemplateResponse</code></a> directly, this\nwill likely result in duplicate code.</p>\n<p>For this reason, Django also provides a number of mixins that provide\nmore discrete functionality. Template rendering, for instance, is\nencapsulated in the\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/mixins-simple/#django.views.generic.base.TemplateResponseMixin\" title=\"django.views.generic.base.TemplateResponseMixin\"><code class=\"xref py py-class docutils literal notranslate\">TemplateResponseMixin</code></a>. The Django\nreference documentation contains <a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/mixins/\"><span class=\"doc\">full documentation of all the\nmixins</span></a>.</p>\n<section id=\"context-and-template-responses\">\n<h2>Context and template responses<a class=\"heading-anchor\" href=\"#context-and-template-responses\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Two central mixins are provided that help in providing a consistent\ninterface to working with templates in class-based views.</p>\n<dl>\n<dt><a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/mixins-simple/#django.views.generic.base.TemplateResponseMixin\" title=\"django.views.generic.base.TemplateResponseMixin\"><code class=\"xref py py-class docutils literal notranslate\">TemplateResponseMixin</code></a></dt><dd><p>Every built in view which returns a\n<a class=\"reference internal\" href=\"/en/1.9/ref/template-response/#django.template.response.TemplateResponse\" title=\"django.template.response.TemplateResponse\"><code class=\"xref py py-class docutils literal notranslate\">TemplateResponse</code></a> will call the\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/mixins-simple/#django.views.generic.base.TemplateResponseMixin.render_to_response\" title=\"django.views.generic.base.TemplateResponseMixin.render_to_response\"><code class=\"xref py py-meth docutils literal notranslate\">render_to_response()</code></a>\nmethod that <code class=\"docutils literal notranslate\">TemplateResponseMixin</code> provides. Most of the time this\nwill be called for you (for instance, it is called by the <code class=\"docutils literal notranslate\">get()</code> method\nimplemented by both <a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/base/#django.views.generic.base.TemplateView\" title=\"django.views.generic.base.TemplateView\"><code class=\"xref py py-class docutils literal notranslate\">TemplateView</code></a> and\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/generic-display/#django.views.generic.detail.DetailView\" title=\"django.views.generic.detail.DetailView\"><code class=\"xref py py-class docutils literal notranslate\">DetailView</code></a>); similarly, it’s unlikely\nthat you’ll need to override it, although if you want your response to\nreturn something not rendered via a Django template then you’ll want to do\nit. For an example of this, see the <a class=\"reference internal\" href=\"#jsonresponsemixin-example\"><span class=\"std std-ref\">JSONResponseMixin example</span></a>.</p>\n<p><code class=\"docutils literal notranslate\">render_to_response()</code> itself calls\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/mixins-simple/#django.views.generic.base.TemplateResponseMixin.get_template_names\" title=\"django.views.generic.base.TemplateResponseMixin.get_template_names\"><code class=\"xref py py-meth docutils literal notranslate\">get_template_names()</code></a>,\nwhich by default will just look up\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/mixins-simple/#django.views.generic.base.TemplateResponseMixin.template_name\" title=\"django.views.generic.base.TemplateResponseMixin.template_name\"><code class=\"xref py py-attr docutils literal notranslate\">template_name</code></a> on\nthe class-based view; two other mixins\n(<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/mixins-single-object/#django.views.generic.detail.SingleObjectTemplateResponseMixin\" title=\"django.views.generic.detail.SingleObjectTemplateResponseMixin\"><code class=\"xref py py-class docutils literal notranslate\">SingleObjectTemplateResponseMixin</code></a>\nand\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/mixins-multiple-object/#django.views.generic.list.MultipleObjectTemplateResponseMixin\" title=\"django.views.generic.list.MultipleObjectTemplateResponseMixin\"><code class=\"xref py py-class docutils literal notranslate\">MultipleObjectTemplateResponseMixin</code></a>)\noverride this to provide more flexible defaults when dealing with actual\nobjects.</p>\n</dd>\n<dt><a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/mixins-simple/#django.views.generic.base.ContextMixin\" title=\"django.views.generic.base.ContextMixin\"><code class=\"xref py py-class docutils literal notranslate\">ContextMixin</code></a></dt><dd><p>Every built in view which needs context data, such as for rendering a\ntemplate (including <code class=\"docutils literal notranslate\">TemplateResponseMixin</code> above), should call\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/mixins-simple/#django.views.generic.base.ContextMixin.get_context_data\" title=\"django.views.generic.base.ContextMixin.get_context_data\"><code class=\"xref py py-meth docutils literal notranslate\">get_context_data()</code></a> passing\nany data they want to ensure is in there as keyword arguments.\n<code class=\"docutils literal notranslate\">get_context_data()</code> returns a dictionary; in <code class=\"docutils literal notranslate\">ContextMixin</code> it\nsimply returns its keyword arguments, but it is common to override this to\nadd more members to the dictionary.</p>\n</dd>\n</dl>\n</section>\n<section id=\"building-up-django-s-generic-class-based-views\">\n<h2>Building up Django’s generic class-based views<a class=\"heading-anchor\" href=\"#building-up-django-s-generic-class-based-views\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Let’s look at how two of Django’s generic class-based views are built\nout of mixins providing discrete functionality. We’ll consider\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/generic-display/#django.views.generic.detail.DetailView\" title=\"django.views.generic.detail.DetailView\"><code class=\"xref py py-class docutils literal notranslate\">DetailView</code></a>, which renders a\n“detail” view of an object, and\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/generic-display/#django.views.generic.list.ListView\" title=\"django.views.generic.list.ListView\"><code class=\"xref py py-class docutils literal notranslate\">ListView</code></a>, which will render a list\nof objects, typically from a queryset, and optionally paginate\nthem. This will introduce us to four mixins which between them provide\nuseful functionality when working with either a single Django object,\nor multiple objects.</p>\n<p>There are also mixins involved in the generic edit views\n(<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/generic-editing/#django.views.generic.edit.FormView\" title=\"django.views.generic.edit.FormView\"><code class=\"xref py py-class docutils literal notranslate\">FormView</code></a>, and the model-specific\nviews <a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/generic-editing/#django.views.generic.edit.CreateView\" title=\"django.views.generic.edit.CreateView\"><code class=\"xref py py-class docutils literal notranslate\">CreateView</code></a>,\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/generic-editing/#django.views.generic.edit.UpdateView\" title=\"django.views.generic.edit.UpdateView\"><code class=\"xref py py-class docutils literal notranslate\">UpdateView</code></a> and\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/generic-editing/#django.views.generic.edit.DeleteView\" title=\"django.views.generic.edit.DeleteView\"><code class=\"xref py py-class docutils literal notranslate\">DeleteView</code></a>), and in the\ndate-based generic views. These are\ncovered in the <a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/mixins/\"><span class=\"doc\">mixin reference\ndocumentation</span></a>.</p>\n<section id=\"detailview-working-with-a-single-django-object\">\n<h3><code class=\"docutils literal notranslate\">DetailView</code>: working with a single Django object<a class=\"heading-anchor\" href=\"#detailview-working-with-a-single-django-object\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>To show the detail of an object, we basically need to do two things:\nwe need to look up the object and then we need to make a\n<a class=\"reference internal\" href=\"/en/1.9/ref/template-response/#django.template.response.TemplateResponse\" title=\"django.template.response.TemplateResponse\"><code class=\"xref py py-class docutils literal notranslate\">TemplateResponse</code></a> with a suitable template,\nand that object as context.</p>\n<p>To get the object, <a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/generic-display/#django.views.generic.detail.DetailView\" title=\"django.views.generic.detail.DetailView\"><code class=\"xref py py-class docutils literal notranslate\">DetailView</code></a>\nrelies on <a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/mixins-single-object/#django.views.generic.detail.SingleObjectMixin\" title=\"django.views.generic.detail.SingleObjectMixin\"><code class=\"xref py py-class docutils literal notranslate\">SingleObjectMixin</code></a>,\nwhich provides a\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/mixins-single-object/#django.views.generic.detail.SingleObjectMixin.get_object\" title=\"django.views.generic.detail.SingleObjectMixin.get_object\"><code class=\"xref py py-meth docutils literal notranslate\">get_object()</code></a>\nmethod that figures out the object based on the URL of the request (it\nlooks for <code class=\"docutils literal notranslate\">pk</code> and <code class=\"docutils literal notranslate\">slug</code> keyword arguments as declared in the\nURLConf, and looks the object up either from the\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/mixins-single-object/#django.views.generic.detail.SingleObjectMixin.model\" title=\"django.views.generic.detail.SingleObjectMixin.model\"><code class=\"xref py py-attr docutils literal notranslate\">model</code></a> attribute\non the view, or the\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/mixins-single-object/#django.views.generic.detail.SingleObjectMixin.queryset\" title=\"django.views.generic.detail.SingleObjectMixin.queryset\"><code class=\"xref py py-attr docutils literal notranslate\">queryset</code></a>\nattribute if that’s provided). <code class=\"docutils literal notranslate\">SingleObjectMixin</code> also overrides\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/mixins-simple/#django.views.generic.base.ContextMixin.get_context_data\" title=\"django.views.generic.base.ContextMixin.get_context_data\"><code class=\"xref py py-meth docutils literal notranslate\">get_context_data()</code></a>,\nwhich is used across all Django’s built in class-based views to supply\ncontext data for template renders.</p>\n<p>To then make a <a class=\"reference internal\" href=\"/en/1.9/ref/template-response/#django.template.response.TemplateResponse\" title=\"django.template.response.TemplateResponse\"><code class=\"xref py py-class docutils literal notranslate\">TemplateResponse</code></a>,\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/flattened-index/#DetailView\" title=\"DetailView\"><code class=\"xref py py-class docutils literal notranslate\">DetailView</code></a> uses\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/mixins-single-object/#django.views.generic.detail.SingleObjectTemplateResponseMixin\" title=\"django.views.generic.detail.SingleObjectTemplateResponseMixin\"><code class=\"xref py py-class docutils literal notranslate\">SingleObjectTemplateResponseMixin</code></a>,\nwhich extends <a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/mixins-simple/#django.views.generic.base.TemplateResponseMixin\" title=\"django.views.generic.base.TemplateResponseMixin\"><code class=\"xref py py-class docutils literal notranslate\">TemplateResponseMixin</code></a>,\noverriding\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/mixins-simple/#django.views.generic.base.TemplateResponseMixin.get_template_names\" title=\"django.views.generic.base.TemplateResponseMixin.get_template_names\"><code class=\"xref py py-meth docutils literal notranslate\">get_template_names()</code></a>\nas discussed above. It actually provides a fairly sophisticated set of options,\nbut the main one that most people are going to use is\n<code class=\"docutils literal notranslate\">&lt;app_label&gt;/&lt;model_name&gt;_detail.html</code>. The <code class=\"docutils literal notranslate\">_detail</code> part can be changed\nby setting\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/mixins-single-object/#django.views.generic.detail.SingleObjectTemplateResponseMixin.template_name_suffix\" title=\"django.views.generic.detail.SingleObjectTemplateResponseMixin.template_name_suffix\"><code class=\"xref py py-attr docutils literal notranslate\">template_name_suffix</code></a>\non a subclass to something else. (For instance, the <a class=\"reference internal\" href=\"/en/1.9/topics/class-based-views/generic-editing/\"><span class=\"doc\">generic edit\nviews</span></a> use <code class=\"docutils literal notranslate\">_form</code> for create and update views, and\n<code class=\"docutils literal notranslate\">_confirm_delete</code> for delete views.)</p>\n</section>\n<section id=\"listview-working-with-many-django-objects\">\n<h3><code class=\"docutils literal notranslate\">ListView</code>: working with many Django objects<a class=\"heading-anchor\" href=\"#listview-working-with-many-django-objects\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Lists of objects follow roughly the same pattern: we need a (possibly\npaginated) list of objects, typically a\n<a class=\"reference internal\" href=\"/en/1.9/ref/models/querysets/#django.db.models.query.QuerySet\" title=\"django.db.models.query.QuerySet\"><code class=\"xref py py-class docutils literal notranslate\">QuerySet</code></a>, and then we need to make a\n<a class=\"reference internal\" href=\"/en/1.9/ref/template-response/#django.template.response.TemplateResponse\" title=\"django.template.response.TemplateResponse\"><code class=\"xref py py-class docutils literal notranslate\">TemplateResponse</code></a> with a suitable template\nusing that list of objects.</p>\n<p>To get the objects, <a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/generic-display/#django.views.generic.list.ListView\" title=\"django.views.generic.list.ListView\"><code class=\"xref py py-class docutils literal notranslate\">ListView</code></a> uses\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/mixins-multiple-object/#django.views.generic.list.MultipleObjectMixin\" title=\"django.views.generic.list.MultipleObjectMixin\"><code class=\"xref py py-class docutils literal notranslate\">MultipleObjectMixin</code></a>, which\nprovides both\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/mixins-multiple-object/#django.views.generic.list.MultipleObjectMixin.get_queryset\" title=\"django.views.generic.list.MultipleObjectMixin.get_queryset\"><code class=\"xref py py-meth docutils literal notranslate\">get_queryset()</code></a>\nand\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/mixins-multiple-object/#django.views.generic.list.MultipleObjectMixin.paginate_queryset\" title=\"django.views.generic.list.MultipleObjectMixin.paginate_queryset\"><code class=\"xref py py-meth docutils literal notranslate\">paginate_queryset()</code></a>. Unlike\nwith <a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/mixins-single-object/#django.views.generic.detail.SingleObjectMixin\" title=\"django.views.generic.detail.SingleObjectMixin\"><code class=\"xref py py-class docutils literal notranslate\">SingleObjectMixin</code></a>, there’s no need\nto key off parts of the URL to figure out the queryset to work with, so the\ndefault just uses the\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/mixins-multiple-object/#django.views.generic.list.MultipleObjectMixin.queryset\" title=\"django.views.generic.list.MultipleObjectMixin.queryset\"><code class=\"xref py py-attr docutils literal notranslate\">queryset</code></a> or\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/mixins-multiple-object/#django.views.generic.list.MultipleObjectMixin.model\" title=\"django.views.generic.list.MultipleObjectMixin.model\"><code class=\"xref py py-attr docutils literal notranslate\">model</code></a> attribute\non the view class. A common reason to override\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/mixins-multiple-object/#django.views.generic.list.MultipleObjectMixin.get_queryset\" title=\"django.views.generic.list.MultipleObjectMixin.get_queryset\"><code class=\"xref py py-meth docutils literal notranslate\">get_queryset()</code></a>\nhere would be to dynamically vary the objects, such as depending on\nthe current user or to exclude posts in the future for a blog.</p>\n<p><a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/mixins-multiple-object/#django.views.generic.list.MultipleObjectMixin\" title=\"django.views.generic.list.MultipleObjectMixin\"><code class=\"xref py py-class docutils literal notranslate\">MultipleObjectMixin</code></a> also overrides\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/mixins-simple/#django.views.generic.base.ContextMixin.get_context_data\" title=\"django.views.generic.base.ContextMixin.get_context_data\"><code class=\"xref py py-meth docutils literal notranslate\">get_context_data()</code></a> to\ninclude appropriate context variables for pagination (providing\ndummies if pagination is disabled). It relies on <code class=\"docutils literal notranslate\">object_list</code> being\npassed in as a keyword argument, which <a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/flattened-index/#ListView\" title=\"ListView\"><code class=\"xref py py-class docutils literal notranslate\">ListView</code></a> arranges for\nit.</p>\n<p>To make a <a class=\"reference internal\" href=\"/en/1.9/ref/template-response/#django.template.response.TemplateResponse\" title=\"django.template.response.TemplateResponse\"><code class=\"xref py py-class docutils literal notranslate\">TemplateResponse</code></a>,\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/flattened-index/#ListView\" title=\"ListView\"><code class=\"xref py py-class docutils literal notranslate\">ListView</code></a> then uses\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/mixins-multiple-object/#django.views.generic.list.MultipleObjectTemplateResponseMixin\" title=\"django.views.generic.list.MultipleObjectTemplateResponseMixin\"><code class=\"xref py py-class docutils literal notranslate\">MultipleObjectTemplateResponseMixin</code></a>;\nas with <a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/mixins-single-object/#django.views.generic.detail.SingleObjectTemplateResponseMixin\" title=\"django.views.generic.detail.SingleObjectTemplateResponseMixin\"><code class=\"xref py py-class docutils literal notranslate\">SingleObjectTemplateResponseMixin</code></a>\nabove, this overrides <code class=\"docutils literal notranslate\">get_template_names()</code> to provide <a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/mixins-multiple-object/#django.views.generic.list.MultipleObjectTemplateResponseMixin\" title=\"django.views.generic.list.MultipleObjectTemplateResponseMixin\"><code class=\"xref py py-meth docutils literal notranslate\">a range of\noptions</code></a>,\nwith the most commonly-used being\n<code class=\"docutils literal notranslate\">&lt;app_label&gt;/&lt;model_name&gt;_list.html</code>, with the <code class=\"docutils literal notranslate\">_list</code> part again\nbeing taken from the\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/mixins-multiple-object/#django.views.generic.list.MultipleObjectTemplateResponseMixin.template_name_suffix\" title=\"django.views.generic.list.MultipleObjectTemplateResponseMixin.template_name_suffix\"><code class=\"xref py py-attr docutils literal notranslate\">template_name_suffix</code></a>\nattribute. (The date based generic views use suffixes such as <code class=\"docutils literal notranslate\">_archive</code>,\n<code class=\"docutils literal notranslate\">_archive_year</code> and so on to use different templates for the various\nspecialized date-based list views.)</p>\n</section>\n</section>\n<section id=\"using-django-s-class-based-view-mixins\">\n<h2>Using Django’s class-based view mixins<a class=\"heading-anchor\" href=\"#using-django-s-class-based-view-mixins\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Now we’ve seen how Django’s generic class-based views use the provided\nmixins, let’s look at other ways we can combine them. Of course we’re\nstill going to be combining them with either built-in class-based\nviews, or other generic class-based views, but there are a range of\nrarer problems you can solve than are provided for by Django out of\nthe box.</p>\n<aside class=\"admonition admonition-warning\" role=\"note\">\n<p class=\"admonition-title\">Warning</p>\n<p>Not all mixins can be used together, and not all generic class\nbased views can be used with all other mixins. Here we present a\nfew examples that do work; if you want to bring together other\nfunctionality then you’ll have to consider interactions between\nattributes and methods that overlap between the different classes\nyou’re using, and how <a class=\"reference external\" href=\"https://www.python.org/download/releases/2.3/mro/\">method resolution order</a> will affect which\nversions of the methods will be called in what order.</p>\n<p>The reference documentation for Django’s <a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/\"><span class=\"doc\">class-based\nviews</span></a> and <a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/mixins/\"><span class=\"doc\">class-based view\nmixins</span></a> will help you in\nunderstanding which attributes and methods are likely to cause\nconflict between different classes and mixins.</p>\n<p>If in doubt, it’s often better to back off and base your work on\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/flattened-index/#View\" title=\"View\"><code class=\"xref py py-class docutils literal notranslate\">View</code></a> or <a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/flattened-index/#TemplateView\" title=\"TemplateView\"><code class=\"xref py py-class docutils literal notranslate\">TemplateView</code></a>, perhaps with\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/mixins-single-object/#django.views.generic.detail.SingleObjectMixin\" title=\"django.views.generic.detail.SingleObjectMixin\"><code class=\"xref py py-class docutils literal notranslate\">SingleObjectMixin</code></a> and\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/mixins-multiple-object/#django.views.generic.list.MultipleObjectMixin\" title=\"django.views.generic.list.MultipleObjectMixin\"><code class=\"xref py py-class docutils literal notranslate\">MultipleObjectMixin</code></a>. Although you\nwill probably end up writing more code, it is more likely to be clearly\nunderstandable to someone else coming to it later, and with fewer\ninteractions to worry about you will save yourself some thinking. (Of\ncourse, you can always dip into Django’s implementation of the generic\nclass-based views for inspiration on how to tackle problems.)</p>\n</aside>\n<section id=\"using-singleobjectmixin-with-view\">\n<h3>Using <code class=\"docutils literal notranslate\">SingleObjectMixin</code> with View<a class=\"heading-anchor\" href=\"#using-singleobjectmixin-with-view\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>If we want to write a simple class-based view that responds only to\n<code class=\"docutils literal notranslate\">POST</code>, we’ll subclass <a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/base/#django.views.generic.base.View\" title=\"django.views.generic.base.View\"><code class=\"xref py py-class docutils literal notranslate\">View</code></a> and\nwrite a <code class=\"docutils literal notranslate\">post()</code> method in the subclass. However if we want our\nprocessing to work on a particular object, identified from the URL,\nwe’ll want the functionality provided by\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/mixins-single-object/#django.views.generic.detail.SingleObjectMixin\" title=\"django.views.generic.detail.SingleObjectMixin\"><code class=\"xref py py-class docutils literal notranslate\">SingleObjectMixin</code></a>.</p>\n<p>We’ll demonstrate this with the <code class=\"docutils literal notranslate\">Author</code> model we used in the\n<a class=\"reference internal\" href=\"/en/1.9/topics/class-based-views/generic-display/\"><span class=\"doc\">generic class-based views introduction</span></a>.</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"default\"><figcaption class=\"code-block-caption\"><code>views.py</code></figcaption><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"views.py\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.http</span> <span class=\"kn\">import</span> HttpResponseForbidden<span class=\"p\">,</span> HttpResponseRedirect\n<span class=\"kn\">from</span> <span class=\"nn\">django.core.urlresolvers</span> <span class=\"kn\">import</span> reverse\n<span class=\"kn\">from</span> <span class=\"nn\">django.views.generic</span> <span class=\"kn\">import</span> View\n<span class=\"kn\">from</span> <span class=\"nn\">django.views.generic.detail</span> <span class=\"kn\">import</span> SingleObjectMixin\n<span class=\"kn\">from</span> <span class=\"nn\">books.models</span> <span class=\"kn\">import</span> Author\n\n<span class=\"k\">class</span> <span class=\"nc\">RecordInterest</span><span class=\"p\">(</span>SingleObjectMixin<span class=\"p\">,</span> View<span class=\"p\">):</span>\n    <span class=\"sd\">&quot;&quot;&quot;Records the current user&#39;s interest in an author.&quot;&quot;&quot;</span>\n    model <span class=\"o\">=</span> Author\n\n    <span class=\"k\">def</span> <span class=\"nf\">post</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> request<span class=\"p\">,</span> <span class=\"o\">*</span>args<span class=\"p\">,</span> <span class=\"o\">**</span>kwargs<span class=\"p\">):</span>\n        <span class=\"k\">if</span> <span class=\"ow\">not</span> request<span class=\"o\">.</span>user<span class=\"o\">.</span>is_authenticated<span class=\"p\">():</span>\n            <span class=\"k\">return</span> HttpResponseForbidden<span class=\"p\">()</span>\n\n        <span class=\"c1\"># Look up the author we&#39;re interested in.</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span>object <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span>get_object<span class=\"p\">()</span>\n        <span class=\"c1\"># Actually record interest somehow here!</span>\n\n        <span class=\"k\">return</span> HttpResponseRedirect<span class=\"p\">(</span>reverse<span class=\"p\">(</span><span class=\"s1\">&#39;author-detail&#39;</span><span class=\"p\">,</span> kwargs<span class=\"o\">=</span><span class=\"p\">{</span><span class=\"s1\">&#39;pk&#39;</span><span class=\"p\">:</span> <span class=\"bp\">self</span><span class=\"o\">.</span>object<span class=\"o\">.</span>pk<span class=\"p\">}))</span>\n</code></pre></figure>\n<p>In practice you’d probably want to record the interest in a key-value\nstore rather than in a relational database, so we’ve left that bit\nout. The only bit of the view that needs to worry about using\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/mixins-single-object/#django.views.generic.detail.SingleObjectMixin\" title=\"django.views.generic.detail.SingleObjectMixin\"><code class=\"xref py py-class docutils literal notranslate\">SingleObjectMixin</code></a> is where we want to\nlook up the author we’re interested in, which it just does with a simple call\nto <code class=\"docutils literal notranslate\">self.get_object()</code>. Everything else is taken care of for us by the\nmixin.</p>\n<p>We can hook this into our URLs easily enough:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"default\"><figcaption class=\"code-block-caption\"><code>urls.py</code></figcaption><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"urls.py\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.conf.urls</span> <span class=\"kn\">import</span> url\n<span class=\"kn\">from</span> <span class=\"nn\">books.views</span> <span class=\"kn\">import</span> RecordInterest\n\nurlpatterns <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"c1\">#...</span>\n    url<span class=\"p\">(</span><span class=\"sa\">r</span><span class=\"s1\">&#39;^author/(?P&lt;pk&gt;[0-9]+)/interest/$&#39;</span><span class=\"p\">,</span> RecordInterest<span class=\"o\">.</span>as_view<span class=\"p\">(),</span> name<span class=\"o\">=</span><span class=\"s1\">&#39;author-interest&#39;</span><span class=\"p\">),</span>\n<span class=\"p\">]</span>\n</code></pre></figure>\n<p>Note the <code class=\"docutils literal notranslate\">pk</code> named group, which\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/mixins-single-object/#django.views.generic.detail.SingleObjectMixin.get_object\" title=\"django.views.generic.detail.SingleObjectMixin.get_object\"><code class=\"xref py py-meth docutils literal notranslate\">get_object()</code></a> uses\nto look up the <code class=\"docutils literal notranslate\">Author</code> instance. You could also use a slug, or\nany of the other features of\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/mixins-single-object/#django.views.generic.detail.SingleObjectMixin\" title=\"django.views.generic.detail.SingleObjectMixin\"><code class=\"xref py py-class docutils literal notranslate\">SingleObjectMixin</code></a>.</p>\n</section>\n<section id=\"using-singleobjectmixin-with-listview\">\n<h3>Using <code class=\"docutils literal notranslate\">SingleObjectMixin</code> with <code class=\"docutils literal notranslate\">ListView</code><a class=\"heading-anchor\" href=\"#using-singleobjectmixin-with-listview\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p><a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/generic-display/#django.views.generic.list.ListView\" title=\"django.views.generic.list.ListView\"><code class=\"xref py py-class docutils literal notranslate\">ListView</code></a> provides built-in\npagination, but you might want to paginate a list of objects that are\nall linked (by a foreign key) to another object. In our publishing\nexample, you might want to paginate through all the books by a\nparticular publisher.</p>\n<p>One way to do this is to combine <a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/flattened-index/#ListView\" title=\"ListView\"><code class=\"xref py py-class docutils literal notranslate\">ListView</code></a> with\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/mixins-single-object/#django.views.generic.detail.SingleObjectMixin\" title=\"django.views.generic.detail.SingleObjectMixin\"><code class=\"xref py py-class docutils literal notranslate\">SingleObjectMixin</code></a>, so that the queryset\nfor the paginated list of books can hang off the publisher found as the single\nobject. In order to do this, we need to have two different querysets:</p>\n<dl class=\"simple\">\n<dt><code class=\"docutils literal notranslate\">Book</code> queryset for use by <a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/generic-display/#django.views.generic.list.ListView\" title=\"django.views.generic.list.ListView\"><code class=\"xref py py-class docutils literal notranslate\">ListView</code></a></dt><dd><p>Since we have access to the <code class=\"docutils literal notranslate\">Publisher</code> whose books we want to list, we\nsimply override <code class=\"docutils literal notranslate\">get_queryset()</code> and use the <code class=\"docutils literal notranslate\">Publisher</code>’s\n<a class=\"reference internal\" href=\"/en/1.9/topics/db/queries/#backwards-related-objects\"><span class=\"std std-ref\">reverse foreign key manager</span></a>.</p>\n</dd>\n<dt><code class=\"docutils literal notranslate\">Publisher</code> queryset for use in <a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/mixins-single-object/#django.views.generic.detail.SingleObjectMixin.get_object\" title=\"django.views.generic.detail.SingleObjectMixin.get_object\"><code class=\"xref py py-meth docutils literal notranslate\">get_object()</code></a></dt><dd><p>We’ll rely on the default implementation of <code class=\"docutils literal notranslate\">get_object()</code> to fetch the\ncorrect <code class=\"docutils literal notranslate\">Publisher</code> object.\nHowever, we need to explicitly pass a <code class=\"docutils literal notranslate\">queryset</code> argument because\notherwise the default implementation of <code class=\"docutils literal notranslate\">get_object()</code> would call\n<code class=\"docutils literal notranslate\">get_queryset()</code> which we have overridden to return <code class=\"docutils literal notranslate\">Book</code> objects\ninstead of <code class=\"docutils literal notranslate\">Publisher</code> ones.</p>\n</dd>\n</dl>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>We have to think carefully about <code class=\"docutils literal notranslate\">get_context_data()</code>.\nSince both <a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/mixins-single-object/#django.views.generic.detail.SingleObjectMixin\" title=\"django.views.generic.detail.SingleObjectMixin\"><code class=\"xref py py-class docutils literal notranslate\">SingleObjectMixin</code></a> and\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/flattened-index/#ListView\" title=\"ListView\"><code class=\"xref py py-class docutils literal notranslate\">ListView</code></a> will\nput things in the context data under the value of\n<code class=\"docutils literal notranslate\">context_object_name</code> if it’s set, we’ll instead explicitly\nensure the <code class=\"docutils literal notranslate\">Publisher</code> is in the context data. <a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/flattened-index/#ListView\" title=\"ListView\"><code class=\"xref py py-class docutils literal notranslate\">ListView</code></a>\nwill add in the suitable <code class=\"docutils literal notranslate\">page_obj</code> and <code class=\"docutils literal notranslate\">paginator</code> for us\nproviding we remember to call <code class=\"docutils literal notranslate\">super()</code>.</p>\n</aside>\n<p>Now we can write a new <code class=\"docutils literal notranslate\">PublisherDetail</code>:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.views.generic</span> <span class=\"kn\">import</span> ListView\n<span class=\"kn\">from</span> <span class=\"nn\">django.views.generic.detail</span> <span class=\"kn\">import</span> SingleObjectMixin\n<span class=\"kn\">from</span> <span class=\"nn\">books.models</span> <span class=\"kn\">import</span> Publisher\n\n<span class=\"k\">class</span> <span class=\"nc\">PublisherDetail</span><span class=\"p\">(</span>SingleObjectMixin<span class=\"p\">,</span> ListView<span class=\"p\">):</span>\n    paginate_by <span class=\"o\">=</span> <span class=\"mi\">2</span>\n    template_name <span class=\"o\">=</span> <span class=\"s2\">&quot;books/publisher_detail.html&quot;</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">get</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> request<span class=\"p\">,</span> <span class=\"o\">*</span>args<span class=\"p\">,</span> <span class=\"o\">**</span>kwargs<span class=\"p\">):</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span>object <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span>get_object<span class=\"p\">(</span>queryset<span class=\"o\">=</span>Publisher<span class=\"o\">.</span>objects<span class=\"o\">.</span>all<span class=\"p\">())</span>\n        <span class=\"k\">return</span> <span class=\"nb\">super</span><span class=\"p\">(</span>PublisherDetail<span class=\"p\">,</span> <span class=\"bp\">self</span><span class=\"p\">)</span><span class=\"o\">.</span>get<span class=\"p\">(</span>request<span class=\"p\">,</span> <span class=\"o\">*</span>args<span class=\"p\">,</span> <span class=\"o\">**</span>kwargs<span class=\"p\">)</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">get_context_data</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"o\">**</span>kwargs<span class=\"p\">):</span>\n        context <span class=\"o\">=</span> <span class=\"nb\">super</span><span class=\"p\">(</span>PublisherDetail<span class=\"p\">,</span> <span class=\"bp\">self</span><span class=\"p\">)</span><span class=\"o\">.</span>get_context_data<span class=\"p\">(</span><span class=\"o\">**</span>kwargs<span class=\"p\">)</span>\n        context<span class=\"p\">[</span><span class=\"s1\">&#39;publisher&#39;</span><span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span>object\n        <span class=\"k\">return</span> context\n\n    <span class=\"k\">def</span> <span class=\"nf\">get_queryset</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"k\">return</span> <span class=\"bp\">self</span><span class=\"o\">.</span>object<span class=\"o\">.</span>book_set<span class=\"o\">.</span>all<span class=\"p\">()</span>\n</code></pre></div>\n<p>Notice how we set <code class=\"docutils literal notranslate\">self.object</code> within <code class=\"docutils literal notranslate\">get()</code> so we\ncan use it again later in <code class=\"docutils literal notranslate\">get_context_data()</code> and <code class=\"docutils literal notranslate\">get_queryset()</code>.\nIf you don’t set <code class=\"docutils literal notranslate\">template_name</code>, the template will default to the normal\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/flattened-index/#ListView\" title=\"ListView\"><code class=\"xref py py-class docutils literal notranslate\">ListView</code></a> choice, which in this case would be\n<code class=\"docutils literal notranslate\">&quot;books/book_list.html&quot;</code> because it’s a list of books;\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/flattened-index/#ListView\" title=\"ListView\"><code class=\"xref py py-class docutils literal notranslate\">ListView</code></a> knows nothing about\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/mixins-single-object/#django.views.generic.detail.SingleObjectMixin\" title=\"django.views.generic.detail.SingleObjectMixin\"><code class=\"xref py py-class docutils literal notranslate\">SingleObjectMixin</code></a>, so it doesn’t have\nany clue this view is anything to do with a <code class=\"docutils literal notranslate\">Publisher</code>.</p>\n<p>The <code class=\"docutils literal notranslate\">paginate_by</code> is deliberately small in the example so you don’t\nhave to create lots of books to see the pagination working! Here’s the\ntemplate you’d want to use:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code><span class=\"cp\">{%</span> <span class=\"k\">extends</span> <span class=\"s2\">&quot;base.html&quot;</span> <span class=\"cp\">%}</span>\n\n<span class=\"cp\">{%</span> <span class=\"k\">block</span> <span class=\"nv\">content</span> <span class=\"cp\">%}</span>\n    <span class=\"p\">&lt;</span><span class=\"nt\">h2</span><span class=\"p\">&gt;</span>Publisher <span class=\"cp\">{{</span> <span class=\"nv\">publisher.name</span> <span class=\"cp\">}}</span><span class=\"p\">&lt;/</span><span class=\"nt\">h2</span><span class=\"p\">&gt;</span>\n\n    <span class=\"p\">&lt;</span><span class=\"nt\">ol</span><span class=\"p\">&gt;</span>\n      <span class=\"cp\">{%</span> <span class=\"k\">for</span> <span class=\"nv\">book</span> <span class=\"k\">in</span> <span class=\"nv\">page_obj</span> <span class=\"cp\">%}</span>\n        <span class=\"p\">&lt;</span><span class=\"nt\">li</span><span class=\"p\">&gt;</span><span class=\"cp\">{{</span> <span class=\"nv\">book.title</span> <span class=\"cp\">}}</span><span class=\"p\">&lt;/</span><span class=\"nt\">li</span><span class=\"p\">&gt;</span>\n      <span class=\"cp\">{%</span> <span class=\"k\">endfor</span> <span class=\"cp\">%}</span>\n    <span class=\"p\">&lt;/</span><span class=\"nt\">ol</span><span class=\"p\">&gt;</span>\n\n    <span class=\"p\">&lt;</span><span class=\"nt\">div</span> <span class=\"na\">class</span><span class=\"o\">=</span><span class=\"s\">&quot;pagination&quot;</span><span class=\"p\">&gt;</span>\n        <span class=\"p\">&lt;</span><span class=\"nt\">span</span> <span class=\"na\">class</span><span class=\"o\">=</span><span class=\"s\">&quot;step-links&quot;</span><span class=\"p\">&gt;</span>\n            <span class=\"cp\">{%</span> <span class=\"k\">if</span> <span class=\"nv\">page_obj.has_previous</span> <span class=\"cp\">%}</span>\n                <span class=\"p\">&lt;</span><span class=\"nt\">a</span> <span class=\"na\">href</span><span class=\"o\">=</span><span class=\"s\">&quot;?page=</span><span class=\"cp\">{{</span> <span class=\"nv\">page_obj.previous_page_number</span> <span class=\"cp\">}}</span><span class=\"s\">&quot;</span><span class=\"p\">&gt;</span>previous<span class=\"p\">&lt;/</span><span class=\"nt\">a</span><span class=\"p\">&gt;</span>\n            <span class=\"cp\">{%</span> <span class=\"k\">endif</span> <span class=\"cp\">%}</span>\n\n            <span class=\"p\">&lt;</span><span class=\"nt\">span</span> <span class=\"na\">class</span><span class=\"o\">=</span><span class=\"s\">&quot;current&quot;</span><span class=\"p\">&gt;</span>\n                Page <span class=\"cp\">{{</span> <span class=\"nv\">page_obj.number</span> <span class=\"cp\">}}</span> of <span class=\"cp\">{{</span> <span class=\"nv\">paginator.num_pages</span> <span class=\"cp\">}}</span>.\n            <span class=\"p\">&lt;/</span><span class=\"nt\">span</span><span class=\"p\">&gt;</span>\n\n            <span class=\"cp\">{%</span> <span class=\"k\">if</span> <span class=\"nv\">page_obj.has_next</span> <span class=\"cp\">%}</span>\n                <span class=\"p\">&lt;</span><span class=\"nt\">a</span> <span class=\"na\">href</span><span class=\"o\">=</span><span class=\"s\">&quot;?page=</span><span class=\"cp\">{{</span> <span class=\"nv\">page_obj.next_page_number</span> <span class=\"cp\">}}</span><span class=\"s\">&quot;</span><span class=\"p\">&gt;</span>next<span class=\"p\">&lt;/</span><span class=\"nt\">a</span><span class=\"p\">&gt;</span>\n            <span class=\"cp\">{%</span> <span class=\"k\">endif</span> <span class=\"cp\">%}</span>\n        <span class=\"p\">&lt;/</span><span class=\"nt\">span</span><span class=\"p\">&gt;</span>\n    <span class=\"p\">&lt;/</span><span class=\"nt\">div</span><span class=\"p\">&gt;</span>\n<span class=\"cp\">{%</span> <span class=\"k\">endblock</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n</section>\n</section>\n<section id=\"avoid-anything-more-complex\">\n<h2>Avoid anything more complex<a class=\"heading-anchor\" href=\"#avoid-anything-more-complex\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Generally you can use\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/mixins-simple/#django.views.generic.base.TemplateResponseMixin\" title=\"django.views.generic.base.TemplateResponseMixin\"><code class=\"xref py py-class docutils literal notranslate\">TemplateResponseMixin</code></a> and\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/mixins-single-object/#django.views.generic.detail.SingleObjectMixin\" title=\"django.views.generic.detail.SingleObjectMixin\"><code class=\"xref py py-class docutils literal notranslate\">SingleObjectMixin</code></a> when you need\ntheir functionality. As shown above, with a bit of care you can even\ncombine <code class=\"docutils literal notranslate\">SingleObjectMixin</code> with\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/generic-display/#django.views.generic.list.ListView\" title=\"django.views.generic.list.ListView\"><code class=\"xref py py-class docutils literal notranslate\">ListView</code></a>. However things get\nincreasingly complex as you try to do so, and a good rule of thumb is:</p>\n<aside class=\"admonition admonition-hint\" role=\"note\">\n<p class=\"admonition-title\">Hint</p>\n<p>Each of your views should use only mixins or views from one of the\ngroups of generic class-based views: <a class=\"reference internal\" href=\"/en/1.9/topics/class-based-views/generic-display/\"><span class=\"doc\">detail,\nlist</span></a>, <a class=\"reference internal\" href=\"/en/1.9/topics/class-based-views/generic-editing/\"><span class=\"doc\">editing</span></a> and\ndate. For example it’s fine to combine\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/flattened-index/#TemplateView\" title=\"TemplateView\"><code class=\"xref py py-class docutils literal notranslate\">TemplateView</code></a> (built in view) with\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/mixins-multiple-object/#django.views.generic.list.MultipleObjectMixin\" title=\"django.views.generic.list.MultipleObjectMixin\"><code class=\"xref py py-class docutils literal notranslate\">MultipleObjectMixin</code></a> (generic list), but\nyou’re likely to have problems combining <code class=\"docutils literal notranslate\">SingleObjectMixin</code> (generic\ndetail) with <code class=\"docutils literal notranslate\">MultipleObjectMixin</code> (generic list).</p>\n</aside>\n<p>To show what happens when you try to get more sophisticated, we show\nan example that sacrifices readability and maintainability when there\nis a simpler solution. First, let’s look at a naive attempt to combine\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/generic-display/#django.views.generic.detail.DetailView\" title=\"django.views.generic.detail.DetailView\"><code class=\"xref py py-class docutils literal notranslate\">DetailView</code></a> with\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/mixins-editing/#django.views.generic.edit.FormMixin\" title=\"django.views.generic.edit.FormMixin\"><code class=\"xref py py-class docutils literal notranslate\">FormMixin</code></a> to enable us to\n<code class=\"docutils literal notranslate\">POST</code> a Django <a class=\"reference internal\" href=\"/en/1.9/ref/forms/api/#django.forms.Form\" title=\"django.forms.Form\"><code class=\"xref py py-class docutils literal notranslate\">Form</code></a> to the same URL as we’re\ndisplaying an object using <a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/flattened-index/#DetailView\" title=\"DetailView\"><code class=\"xref py py-class docutils literal notranslate\">DetailView</code></a>.</p>\n<section id=\"using-formmixin-with-detailview\">\n<h3>Using <code class=\"docutils literal notranslate\">FormMixin</code> with <code class=\"docutils literal notranslate\">DetailView</code><a class=\"heading-anchor\" href=\"#using-formmixin-with-detailview\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Think back to our earlier example of using <a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/flattened-index/#View\" title=\"View\"><code class=\"xref py py-class docutils literal notranslate\">View</code></a> and\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/mixins-single-object/#django.views.generic.detail.SingleObjectMixin\" title=\"django.views.generic.detail.SingleObjectMixin\"><code class=\"xref py py-class docutils literal notranslate\">SingleObjectMixin</code></a> together. We were\nrecording a user’s interest in a particular author; say now that we want to\nlet them leave a message saying why they like them. Again, let’s assume we’re\nnot going to store this in a relational database but instead in\nsomething more esoteric that we won’t worry about here.</p>\n<p>At this point it’s natural to reach for a <a class=\"reference internal\" href=\"/en/1.9/ref/forms/api/#django.forms.Form\" title=\"django.forms.Form\"><code class=\"xref py py-class docutils literal notranslate\">Form</code></a> to\nencapsulate the information sent from the user’s browser to Django. Say also\nthat we’re heavily invested in <a class=\"reference external\" href=\"https://en.wikipedia.org/wiki/Representational_state_transfer\">REST</a>, so we want to use the same URL for\ndisplaying the author as for capturing the message from the\nuser. Let’s rewrite our <code class=\"docutils literal notranslate\">AuthorDetailView</code> to do that.</p>\n<p>We’ll keep the <code class=\"docutils literal notranslate\">GET</code> handling from <a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/flattened-index/#DetailView\" title=\"DetailView\"><code class=\"xref py py-class docutils literal notranslate\">DetailView</code></a>, although\nwe’ll have to add a <a class=\"reference internal\" href=\"/en/1.9/ref/forms/api/#django.forms.Form\" title=\"django.forms.Form\"><code class=\"xref py py-class docutils literal notranslate\">Form</code></a> into the context data so we can\nrender it in the template. We’ll also want to pull in form processing\nfrom <a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/mixins-editing/#django.views.generic.edit.FormMixin\" title=\"django.views.generic.edit.FormMixin\"><code class=\"xref py py-class docutils literal notranslate\">FormMixin</code></a>, and write a bit of\ncode so that on <code class=\"docutils literal notranslate\">POST</code> the form gets called appropriately.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>We use <a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/mixins-editing/#django.views.generic.edit.FormMixin\" title=\"django.views.generic.edit.FormMixin\"><code class=\"xref py py-class docutils literal notranslate\">FormMixin</code></a> and implement\n<code class=\"docutils literal notranslate\">post()</code> ourselves rather than try to mix <a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/flattened-index/#DetailView\" title=\"DetailView\"><code class=\"xref py py-class docutils literal notranslate\">DetailView</code></a> with\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/flattened-index/#FormView\" title=\"FormView\"><code class=\"xref py py-class docutils literal notranslate\">FormView</code></a> (which provides a suitable <code class=\"docutils literal notranslate\">post()</code> already) because\nboth of the views implement <code class=\"docutils literal notranslate\">get()</code>, and things would get much more\nconfusing.</p>\n</aside>\n<p>Our new <code class=\"docutils literal notranslate\">AuthorDetail</code> looks like this:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"c1\"># CAUTION: you almost certainly do not want to do this.</span>\n<span class=\"c1\"># It is provided as part of a discussion of problems you can</span>\n<span class=\"c1\"># run into when combining different generic class-based view</span>\n<span class=\"c1\"># functionality that is not designed to be used together.</span>\n\n<span class=\"kn\">from</span> <span class=\"nn\">django</span> <span class=\"kn\">import</span> forms\n<span class=\"kn\">from</span> <span class=\"nn\">django.http</span> <span class=\"kn\">import</span> HttpResponseForbidden\n<span class=\"kn\">from</span> <span class=\"nn\">django.core.urlresolvers</span> <span class=\"kn\">import</span> reverse\n<span class=\"kn\">from</span> <span class=\"nn\">django.views.generic</span> <span class=\"kn\">import</span> DetailView\n<span class=\"kn\">from</span> <span class=\"nn\">django.views.generic.edit</span> <span class=\"kn\">import</span> FormMixin\n<span class=\"kn\">from</span> <span class=\"nn\">books.models</span> <span class=\"kn\">import</span> Author\n\n<span class=\"k\">class</span> <span class=\"nc\">AuthorInterestForm</span><span class=\"p\">(</span>forms<span class=\"o\">.</span>Form<span class=\"p\">):</span>\n    message <span class=\"o\">=</span> forms<span class=\"o\">.</span>CharField<span class=\"p\">()</span>\n\n<span class=\"k\">class</span> <span class=\"nc\">AuthorDetail</span><span class=\"p\">(</span>FormMixin<span class=\"p\">,</span> DetailView<span class=\"p\">):</span>\n    model <span class=\"o\">=</span> Author\n    form_class <span class=\"o\">=</span> AuthorInterestForm\n\n    <span class=\"k\">def</span> <span class=\"nf\">get_success_url</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"k\">return</span> reverse<span class=\"p\">(</span><span class=\"s1\">&#39;author-detail&#39;</span><span class=\"p\">,</span> kwargs<span class=\"o\">=</span><span class=\"p\">{</span><span class=\"s1\">&#39;pk&#39;</span><span class=\"p\">:</span> <span class=\"bp\">self</span><span class=\"o\">.</span>object<span class=\"o\">.</span>pk<span class=\"p\">})</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">get_context_data</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"o\">**</span>kwargs<span class=\"p\">):</span>\n        context <span class=\"o\">=</span> <span class=\"nb\">super</span><span class=\"p\">(</span>AuthorDetail<span class=\"p\">,</span> <span class=\"bp\">self</span><span class=\"p\">)</span><span class=\"o\">.</span>get_context_data<span class=\"p\">(</span><span class=\"o\">**</span>kwargs<span class=\"p\">)</span>\n        context<span class=\"p\">[</span><span class=\"s1\">&#39;form&#39;</span><span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span>get_form<span class=\"p\">()</span>\n        <span class=\"k\">return</span> context\n\n    <span class=\"k\">def</span> <span class=\"nf\">post</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> request<span class=\"p\">,</span> <span class=\"o\">*</span>args<span class=\"p\">,</span> <span class=\"o\">**</span>kwargs<span class=\"p\">):</span>\n        <span class=\"k\">if</span> <span class=\"ow\">not</span> request<span class=\"o\">.</span>user<span class=\"o\">.</span>is_authenticated<span class=\"p\">():</span>\n            <span class=\"k\">return</span> HttpResponseForbidden<span class=\"p\">()</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span>object <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span>get_object<span class=\"p\">()</span>\n        form <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span>get_form<span class=\"p\">()</span>\n        <span class=\"k\">if</span> form<span class=\"o\">.</span>is_valid<span class=\"p\">():</span>\n            <span class=\"k\">return</span> <span class=\"bp\">self</span><span class=\"o\">.</span>form_valid<span class=\"p\">(</span>form<span class=\"p\">)</span>\n        <span class=\"k\">else</span><span class=\"p\">:</span>\n            <span class=\"k\">return</span> <span class=\"bp\">self</span><span class=\"o\">.</span>form_invalid<span class=\"p\">(</span>form<span class=\"p\">)</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">form_valid</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> form<span class=\"p\">):</span>\n        <span class=\"c1\"># Here, we would record the user&#39;s interest using the message</span>\n        <span class=\"c1\"># passed in form.cleaned_data[&#39;message&#39;]</span>\n        <span class=\"k\">return</span> <span class=\"nb\">super</span><span class=\"p\">(</span>AuthorDetail<span class=\"p\">,</span> <span class=\"bp\">self</span><span class=\"p\">)</span><span class=\"o\">.</span>form_valid<span class=\"p\">(</span>form<span class=\"p\">)</span>\n</code></pre></div>\n<p><code class=\"docutils literal notranslate\">get_success_url()</code> is just providing somewhere to redirect to,\nwhich gets used in the default implementation of\n<code class=\"docutils literal notranslate\">form_valid()</code>. We have to provide our own <code class=\"docutils literal notranslate\">post()</code> as\nnoted earlier, and override <code class=\"docutils literal notranslate\">get_context_data()</code> to make the\n<a class=\"reference internal\" href=\"/en/1.9/ref/forms/api/#django.forms.Form\" title=\"django.forms.Form\"><code class=\"xref py py-class docutils literal notranslate\">Form</code></a> available in the context data.</p>\n</section>\n<section id=\"a-better-solution\">\n<h3>A better solution<a class=\"heading-anchor\" href=\"#a-better-solution\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>It should be obvious that the number of subtle interactions between\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/mixins-editing/#django.views.generic.edit.FormMixin\" title=\"django.views.generic.edit.FormMixin\"><code class=\"xref py py-class docutils literal notranslate\">FormMixin</code></a> and <a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/flattened-index/#DetailView\" title=\"DetailView\"><code class=\"xref py py-class docutils literal notranslate\">DetailView</code></a> is\nalready testing our ability to manage things. It’s unlikely you’d want to\nwrite this kind of class yourself.</p>\n<p>In this case, it would be fairly easy to just write the <code class=\"docutils literal notranslate\">post()</code>\nmethod yourself, keeping <a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/flattened-index/#DetailView\" title=\"DetailView\"><code class=\"xref py py-class docutils literal notranslate\">DetailView</code></a> as the only generic\nfunctionality, although writing <a class=\"reference internal\" href=\"/en/1.9/ref/forms/api/#django.forms.Form\" title=\"django.forms.Form\"><code class=\"xref py py-class docutils literal notranslate\">Form</code></a> handling code\ninvolves a lot of duplication.</p>\n<p>Alternatively, it would still be easier than the above approach to\nhave a separate view for processing the form, which could use\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/generic-editing/#django.views.generic.edit.FormView\" title=\"django.views.generic.edit.FormView\"><code class=\"xref py py-class docutils literal notranslate\">FormView</code></a> distinct from\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/flattened-index/#DetailView\" title=\"DetailView\"><code class=\"xref py py-class docutils literal notranslate\">DetailView</code></a> without concerns.</p>\n</section>\n<section id=\"an-alternative-better-solution\">\n<h3>An alternative better solution<a class=\"heading-anchor\" href=\"#an-alternative-better-solution\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>What we’re really trying to do here is to use two different class\nbased views from the same URL. So why not do just that? We have a very\nclear division here: <code class=\"docutils literal notranslate\">GET</code> requests should get the\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/flattened-index/#DetailView\" title=\"DetailView\"><code class=\"xref py py-class docutils literal notranslate\">DetailView</code></a> (with the <a class=\"reference internal\" href=\"/en/1.9/ref/forms/api/#django.forms.Form\" title=\"django.forms.Form\"><code class=\"xref py py-class docutils literal notranslate\">Form</code></a> added to the context\ndata), and <code class=\"docutils literal notranslate\">POST</code> requests should get the <a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/flattened-index/#FormView\" title=\"FormView\"><code class=\"xref py py-class docutils literal notranslate\">FormView</code></a>. Let’s\nset up those views first.</p>\n<p>The <code class=\"docutils literal notranslate\">AuthorDisplay</code> view is almost the same as <a class=\"reference internal\" href=\"/en/1.9/topics/class-based-views/generic-display/#generic-views-extra-work\"><span class=\"std std-ref\">when we\nfirst introduced AuthorDetail</span></a>; we have to\nwrite our own <code class=\"docutils literal notranslate\">get_context_data()</code> to make the\n<code class=\"docutils literal notranslate\">AuthorInterestForm</code> available to the template. We’ll skip the\n<code class=\"docutils literal notranslate\">get_object()</code> override from before for clarity:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.views.generic</span> <span class=\"kn\">import</span> DetailView\n<span class=\"kn\">from</span> <span class=\"nn\">django</span> <span class=\"kn\">import</span> forms\n<span class=\"kn\">from</span> <span class=\"nn\">books.models</span> <span class=\"kn\">import</span> Author\n\n<span class=\"k\">class</span> <span class=\"nc\">AuthorInterestForm</span><span class=\"p\">(</span>forms<span class=\"o\">.</span>Form<span class=\"p\">):</span>\n    message <span class=\"o\">=</span> forms<span class=\"o\">.</span>CharField<span class=\"p\">()</span>\n\n<span class=\"k\">class</span> <span class=\"nc\">AuthorDisplay</span><span class=\"p\">(</span>DetailView<span class=\"p\">):</span>\n    model <span class=\"o\">=</span> Author\n\n    <span class=\"k\">def</span> <span class=\"nf\">get_context_data</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"o\">**</span>kwargs<span class=\"p\">):</span>\n        context <span class=\"o\">=</span> <span class=\"nb\">super</span><span class=\"p\">(</span>AuthorDisplay<span class=\"p\">,</span> <span class=\"bp\">self</span><span class=\"p\">)</span><span class=\"o\">.</span>get_context_data<span class=\"p\">(</span><span class=\"o\">**</span>kwargs<span class=\"p\">)</span>\n        context<span class=\"p\">[</span><span class=\"s1\">&#39;form&#39;</span><span class=\"p\">]</span> <span class=\"o\">=</span> AuthorInterestForm<span class=\"p\">()</span>\n        <span class=\"k\">return</span> context\n</code></pre></div>\n<p>Then the <code class=\"docutils literal notranslate\">AuthorInterest</code> is a simple <a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/flattened-index/#FormView\" title=\"FormView\"><code class=\"xref py py-class docutils literal notranslate\">FormView</code></a>, but we\nhave to bring in <a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/mixins-single-object/#django.views.generic.detail.SingleObjectMixin\" title=\"django.views.generic.detail.SingleObjectMixin\"><code class=\"xref py py-class docutils literal notranslate\">SingleObjectMixin</code></a> so we\ncan find the author we’re talking about, and we have to remember to set\n<code class=\"docutils literal notranslate\">template_name</code> to ensure that form errors will render the same\ntemplate as <code class=\"docutils literal notranslate\">AuthorDisplay</code> is using on <code class=\"docutils literal notranslate\">GET</code>:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.core.urlresolvers</span> <span class=\"kn\">import</span> reverse\n<span class=\"kn\">from</span> <span class=\"nn\">django.http</span> <span class=\"kn\">import</span> HttpResponseForbidden\n<span class=\"kn\">from</span> <span class=\"nn\">django.views.generic</span> <span class=\"kn\">import</span> FormView\n<span class=\"kn\">from</span> <span class=\"nn\">django.views.generic.detail</span> <span class=\"kn\">import</span> SingleObjectMixin\n\n<span class=\"k\">class</span> <span class=\"nc\">AuthorInterest</span><span class=\"p\">(</span>SingleObjectMixin<span class=\"p\">,</span> FormView<span class=\"p\">):</span>\n    template_name <span class=\"o\">=</span> <span class=\"s1\">&#39;books/author_detail.html&#39;</span>\n    form_class <span class=\"o\">=</span> AuthorInterestForm\n    model <span class=\"o\">=</span> Author\n\n    <span class=\"k\">def</span> <span class=\"nf\">post</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> request<span class=\"p\">,</span> <span class=\"o\">*</span>args<span class=\"p\">,</span> <span class=\"o\">**</span>kwargs<span class=\"p\">):</span>\n        <span class=\"k\">if</span> <span class=\"ow\">not</span> request<span class=\"o\">.</span>user<span class=\"o\">.</span>is_authenticated<span class=\"p\">():</span>\n            <span class=\"k\">return</span> HttpResponseForbidden<span class=\"p\">()</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span>object <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span>get_object<span class=\"p\">()</span>\n        <span class=\"k\">return</span> <span class=\"nb\">super</span><span class=\"p\">(</span>AuthorInterest<span class=\"p\">,</span> <span class=\"bp\">self</span><span class=\"p\">)</span><span class=\"o\">.</span>post<span class=\"p\">(</span>request<span class=\"p\">,</span> <span class=\"o\">*</span>args<span class=\"p\">,</span> <span class=\"o\">**</span>kwargs<span class=\"p\">)</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">get_success_url</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"k\">return</span> reverse<span class=\"p\">(</span><span class=\"s1\">&#39;author-detail&#39;</span><span class=\"p\">,</span> kwargs<span class=\"o\">=</span><span class=\"p\">{</span><span class=\"s1\">&#39;pk&#39;</span><span class=\"p\">:</span> <span class=\"bp\">self</span><span class=\"o\">.</span>object<span class=\"o\">.</span>pk<span class=\"p\">})</span>\n</code></pre></div>\n<p>Finally we bring this together in a new <code class=\"docutils literal notranslate\">AuthorDetail</code> view. We\nalready know that calling <a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/base/#django.views.generic.base.View.as_view\" title=\"django.views.generic.base.View.as_view\"><code class=\"xref py py-meth docutils literal notranslate\">as_view()</code></a> on\na class-based view gives us something that behaves exactly like a function\nbased view, so we can do that at the point we choose between the two subviews.</p>\n<p>You can of course pass through keyword arguments to\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/base/#django.views.generic.base.View.as_view\" title=\"django.views.generic.base.View.as_view\"><code class=\"xref py py-meth docutils literal notranslate\">as_view()</code></a> in the same way you\nwould in your URLconf, such as if you wanted the <code class=\"docutils literal notranslate\">AuthorInterest</code> behavior\nto also appear at another URL but using a different template:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.views.generic</span> <span class=\"kn\">import</span> View\n\n<span class=\"k\">class</span> <span class=\"nc\">AuthorDetail</span><span class=\"p\">(</span>View<span class=\"p\">):</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">get</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> request<span class=\"p\">,</span> <span class=\"o\">*</span>args<span class=\"p\">,</span> <span class=\"o\">**</span>kwargs<span class=\"p\">):</span>\n        view <span class=\"o\">=</span> AuthorDisplay<span class=\"o\">.</span>as_view<span class=\"p\">()</span>\n        <span class=\"k\">return</span> view<span class=\"p\">(</span>request<span class=\"p\">,</span> <span class=\"o\">*</span>args<span class=\"p\">,</span> <span class=\"o\">**</span>kwargs<span class=\"p\">)</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">post</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> request<span class=\"p\">,</span> <span class=\"o\">*</span>args<span class=\"p\">,</span> <span class=\"o\">**</span>kwargs<span class=\"p\">):</span>\n        view <span class=\"o\">=</span> AuthorInterest<span class=\"o\">.</span>as_view<span class=\"p\">()</span>\n        <span class=\"k\">return</span> view<span class=\"p\">(</span>request<span class=\"p\">,</span> <span class=\"o\">*</span>args<span class=\"p\">,</span> <span class=\"o\">**</span>kwargs<span class=\"p\">)</span>\n</code></pre></div>\n<p>This approach can also be used with any other generic class-based\nviews or your own class-based views inheriting directly from\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/flattened-index/#View\" title=\"View\"><code class=\"xref py py-class docutils literal notranslate\">View</code></a> or <a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/flattened-index/#TemplateView\" title=\"TemplateView\"><code class=\"xref py py-class docutils literal notranslate\">TemplateView</code></a>, as it keeps the different\nviews as separate as possible.</p>\n</section>\n</section>\n<section id=\"more-than-just-html\">\n<span id=\"jsonresponsemixin-example\"></span><h2>More than just HTML<a class=\"heading-anchor\" href=\"#more-than-just-html\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Where class-based views shine is when you want to do the same thing many times.\nSuppose you’re writing an API, and every view should return JSON instead of\nrendered HTML.</p>\n<p>We can create a mixin class to use in all of our views, handling the\nconversion to JSON once.</p>\n<p>For example, a simple JSON mixin might look something like this:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.http</span> <span class=\"kn\">import</span> JsonResponse\n\n<span class=\"k\">class</span> <span class=\"nc\">JSONResponseMixin</span><span class=\"p\">(</span><span class=\"nb\">object</span><span class=\"p\">):</span>\n    <span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">    A mixin that can be used to render a JSON response.</span>\n<span class=\"sd\">    &quot;&quot;&quot;</span>\n    <span class=\"k\">def</span> <span class=\"nf\">render_to_json_response</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> context<span class=\"p\">,</span> <span class=\"o\">**</span>response_kwargs<span class=\"p\">):</span>\n        <span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">        Returns a JSON response, transforming &#39;context&#39; to make the payload.</span>\n<span class=\"sd\">        &quot;&quot;&quot;</span>\n        <span class=\"k\">return</span> JsonResponse<span class=\"p\">(</span>\n            <span class=\"bp\">self</span><span class=\"o\">.</span>get_data<span class=\"p\">(</span>context<span class=\"p\">),</span>\n            <span class=\"o\">**</span>response_kwargs\n        <span class=\"p\">)</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">get_data</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> context<span class=\"p\">):</span>\n        <span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">        Returns an object that will be serialized as JSON by json.dumps().</span>\n<span class=\"sd\">        &quot;&quot;&quot;</span>\n        <span class=\"c1\"># Note: This is *EXTREMELY* naive; in reality, you&#39;ll need</span>\n        <span class=\"c1\"># to do much more complex handling to ensure that arbitrary</span>\n        <span class=\"c1\"># objects -- such as Django model instances or querysets</span>\n        <span class=\"c1\"># -- can be serialized as JSON.</span>\n        <span class=\"k\">return</span> context\n</code></pre></div>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>Check out the <a class=\"reference internal\" href=\"/en/1.9/topics/serialization/\"><span class=\"doc\">Serializing Django objects</span></a> documentation for more\ninformation on how to correctly transform Django models and querysets into\nJSON.</p>\n</aside>\n<p>This mixin provides a <code class=\"docutils literal notranslate\">render_to_json_response()</code> method with the same signature\nas <a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/mixins-simple/#django.views.generic.base.TemplateResponseMixin.render_to_response\" title=\"django.views.generic.base.TemplateResponseMixin.render_to_response\"><code class=\"xref py py-func docutils literal notranslate\">render_to_response()</code></a>.\nTo use it, we simply need to mix it into a <code class=\"docutils literal notranslate\">TemplateView</code> for example,\nand override <code class=\"docutils literal notranslate\">render_to_response()</code> to call <code class=\"docutils literal notranslate\">render_to_json_response()</code> instead:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.views.generic</span> <span class=\"kn\">import</span> TemplateView\n\n<span class=\"k\">class</span> <span class=\"nc\">JSONView</span><span class=\"p\">(</span>JSONResponseMixin<span class=\"p\">,</span> TemplateView<span class=\"p\">):</span>\n    <span class=\"k\">def</span> <span class=\"nf\">render_to_response</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> context<span class=\"p\">,</span> <span class=\"o\">**</span>response_kwargs<span class=\"p\">):</span>\n        <span class=\"k\">return</span> <span class=\"bp\">self</span><span class=\"o\">.</span>render_to_json_response<span class=\"p\">(</span>context<span class=\"p\">,</span> <span class=\"o\">**</span>response_kwargs<span class=\"p\">)</span>\n</code></pre></div>\n<p>Equally we could use our mixin with one of the generic views. We can make our\nown version of <a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/generic-display/#django.views.generic.detail.DetailView\" title=\"django.views.generic.detail.DetailView\"><code class=\"xref py py-class docutils literal notranslate\">DetailView</code></a> by mixing\n<code class=\"docutils literal notranslate\">JSONResponseMixin</code> with the\n<code class=\"docutils literal notranslate\">django.views.generic.detail.BaseDetailView</code> – (the\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/generic-display/#django.views.generic.detail.DetailView\" title=\"django.views.generic.detail.DetailView\"><code class=\"xref py py-class docutils literal notranslate\">DetailView</code></a> before template\nrendering behavior has been mixed in):</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.views.generic.detail</span> <span class=\"kn\">import</span> BaseDetailView\n\n<span class=\"k\">class</span> <span class=\"nc\">JSONDetailView</span><span class=\"p\">(</span>JSONResponseMixin<span class=\"p\">,</span> BaseDetailView<span class=\"p\">):</span>\n    <span class=\"k\">def</span> <span class=\"nf\">render_to_response</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> context<span class=\"p\">,</span> <span class=\"o\">**</span>response_kwargs<span class=\"p\">):</span>\n        <span class=\"k\">return</span> <span class=\"bp\">self</span><span class=\"o\">.</span>render_to_json_response<span class=\"p\">(</span>context<span class=\"p\">,</span> <span class=\"o\">**</span>response_kwargs<span class=\"p\">)</span>\n</code></pre></div>\n<p>This view can then be deployed in the same way as any other\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/generic-display/#django.views.generic.detail.DetailView\" title=\"django.views.generic.detail.DetailView\"><code class=\"xref py py-class docutils literal notranslate\">DetailView</code></a>, with exactly the\nsame behavior – except for the format of the response.</p>\n<p>If you want to be really adventurous, you could even mix a\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/generic-display/#django.views.generic.detail.DetailView\" title=\"django.views.generic.detail.DetailView\"><code class=\"xref py py-class docutils literal notranslate\">DetailView</code></a> subclass that is able\nto return <em>both</em> HTML and JSON content, depending on some property of\nthe HTTP request, such as a query argument or a HTTP header. Just mix\nin both the <code class=\"docutils literal notranslate\">JSONResponseMixin</code> and a\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/mixins-single-object/#django.views.generic.detail.SingleObjectTemplateResponseMixin\" title=\"django.views.generic.detail.SingleObjectTemplateResponseMixin\"><code class=\"xref py py-class docutils literal notranslate\">SingleObjectTemplateResponseMixin</code></a>,\nand override the implementation of\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/mixins-simple/#django.views.generic.base.TemplateResponseMixin.render_to_response\" title=\"django.views.generic.base.TemplateResponseMixin.render_to_response\"><code class=\"xref py py-func docutils literal notranslate\">render_to_response()</code></a>\nto defer to the appropriate rendering method depending on the type of response\nthat the user requested:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.views.generic.detail</span> <span class=\"kn\">import</span> SingleObjectTemplateResponseMixin\n\n<span class=\"k\">class</span> <span class=\"nc\">HybridDetailView</span><span class=\"p\">(</span>JSONResponseMixin<span class=\"p\">,</span> SingleObjectTemplateResponseMixin<span class=\"p\">,</span> BaseDetailView<span class=\"p\">):</span>\n    <span class=\"k\">def</span> <span class=\"nf\">render_to_response</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> context<span class=\"p\">):</span>\n        <span class=\"c1\"># Look for a &#39;format=json&#39; GET argument</span>\n        <span class=\"k\">if</span> <span class=\"bp\">self</span><span class=\"o\">.</span>request<span class=\"o\">.</span>GET<span class=\"o\">.</span>get<span class=\"p\">(</span><span class=\"s1\">&#39;format&#39;</span><span class=\"p\">)</span> <span class=\"o\">==</span> <span class=\"s1\">&#39;json&#39;</span><span class=\"p\">:</span>\n            <span class=\"k\">return</span> <span class=\"bp\">self</span><span class=\"o\">.</span>render_to_json_response<span class=\"p\">(</span>context<span class=\"p\">)</span>\n        <span class=\"k\">else</span><span class=\"p\">:</span>\n            <span class=\"k\">return</span> <span class=\"nb\">super</span><span class=\"p\">(</span>HybridDetailView<span class=\"p\">,</span> <span class=\"bp\">self</span><span class=\"p\">)</span><span class=\"o\">.</span>render_to_response<span class=\"p\">(</span>context<span class=\"p\">)</span>\n</code></pre></div>\n<p>Because of the way that Python resolves method overloading, the call to\n<code class=\"docutils literal notranslate\">super(HybridDetailView, self).render_to_response(context)</code> ends up\ncalling the\n<a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/mixins-simple/#django.views.generic.base.TemplateResponseMixin.render_to_response\" title=\"django.views.generic.base.TemplateResponseMixin.render_to_response\"><code class=\"xref py py-meth docutils literal notranslate\">render_to_response()</code></a>\nimplementation of <a class=\"reference internal\" href=\"/en/1.9/ref/class-based-views/mixins-simple/#django.views.generic.base.TemplateResponseMixin\" title=\"django.views.generic.base.TemplateResponseMixin\"><code class=\"xref py py-class docutils literal notranslate\">TemplateResponseMixin</code></a>.</p>\n</section>","rootId":"using-mixins-with-class-based-views","toc":[{"title":"Context and template responses","anchor":"context-and-template-responses","children":[]},{"title":"Building up Django’s generic class-based views","anchor":"building-up-django-s-generic-class-based-views","children":[{"title":"DetailView: working with a single Django object","anchor":"detailview-working-with-a-single-django-object","children":[]},{"title":"ListView: working with many Django objects","anchor":"listview-working-with-many-django-objects","children":[]}]},{"title":"Using Django’s class-based view mixins","anchor":"using-django-s-class-based-view-mixins","children":[{"title":"Using SingleObjectMixin with View","anchor":"using-singleobjectmixin-with-view","children":[]},{"title":"Using SingleObjectMixin with ListView","anchor":"using-singleobjectmixin-with-listview","children":[]}]},{"title":"Avoid anything more complex","anchor":"avoid-anything-more-complex","children":[{"title":"Using FormMixin with DetailView","anchor":"using-formmixin-with-detailview","children":[]},{"title":"A better solution","anchor":"a-better-solution","children":[]},{"title":"An alternative better solution","anchor":"an-alternative-better-solution","children":[]}]},{"title":"More than just HTML","anchor":"more-than-just-html","children":[]}],"breadcrumbs":[{"docname":"topics/index","title":"Using Django","url":"/en/1.9/topics/"},{"docname":"topics/class-based-views/index","title":"Class-based views","url":"/en/1.9/topics/class-based-views/"}],"prev":{"docname":"topics/class-based-views/generic-editing","title":"Form handling with class-based views","url":"/en/1.9/topics/class-based-views/generic-editing/"},"next":{"docname":"topics/migrations","title":"Migrations","url":"/en/1.9/topics/migrations/"},"formats":{"html":"/en/1.9/topics/class-based-views/mixins/","markdown":"/en/1.9/topics/class-based-views/mixins.md","json":"/en/1.9/topics/class-based-views/mixins.json"},"source":"https://github.com/django/django/blob/stable/1.9.x/docs/topics/class-based-views/mixins.txt","official":"https://docs.djangoproject.com/en/1.9/topics/class-based-views/mixins/","inVersions":["dev","6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0","1.11","1.10","1.9","1.8"],"inLocales":["en","fr","ja","id","pt-br","es"]}