<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Multiple object mixins — Django 1.5.9 documentation</title> <link rel="stylesheet" href="../../_static/default.css" type="text/css" /> <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../../', VERSION: '1.5.9', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </script> <script type="text/javascript" src="../../_static/jquery.js"></script> <script type="text/javascript" src="../../_static/underscore.js"></script> <script type="text/javascript" src="../../_static/doctools.js"></script> <link rel="top" title="Django 1.5.9 documentation" href="../../index.html" /> <link rel="up" title="Class-based views mixins" href="mixins.html" /> <link rel="next" title="Editing mixins" href="mixins-editing.html" /> <link rel="prev" title="Single object mixins" href="mixins-single-object.html" /> <script type="text/javascript" src="../../templatebuiltins.js"></script> <script type="text/javascript"> (function($) { if (!django_template_builtins) { // templatebuiltins.js missing, do nothing. return; } $(document).ready(function() { // Hyperlink Django template tags and filters var base = "../templates/builtins.html"; if (base == "#") { // Special case for builtins.html itself base = ""; } // Tags are keywords, class '.k' $("div.highlight\\-html\\+django span.k").each(function(i, elem) { var tagname = $(elem).text(); if ($.inArray(tagname, django_template_builtins.ttags) != -1) { var fragment = tagname.replace(/_/, '-'); $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>"); } }); // Filters are functions, class '.nf' $("div.highlight\\-html\\+django span.nf").each(function(i, elem) { var filtername = $(elem).text(); if ($.inArray(filtername, django_template_builtins.tfilters) != -1) { var fragment = filtername.replace(/_/, '-'); $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>"); } }); }); })(jQuery); </script> </head> <body> <div class="document"> <div id="custom-doc" class="yui-t6"> <div id="hd"> <h1><a href="../../index.html">Django 1.5.9 documentation</a></h1> <div id="global-nav"> <a title="Home page" href="../../index.html">Home</a> | <a title="Table of contents" href="../../contents.html">Table of contents</a> | <a title="Global index" href="../../genindex.html">Index</a> | <a title="Module index" href="../../py-modindex.html">Modules</a> </div> <div class="nav"> « <a href="mixins-single-object.html" title="Single object mixins">previous</a> | <a href="../index.html" title="API Reference" accesskey="U">up</a> | <a href="mixins-editing.html" title="Editing mixins">next</a> »</div> </div> <div id="bd"> <div id="yui-main"> <div class="yui-b"> <div class="yui-g" id="ref-class-based-views-mixins-multiple-object"> <div class="section" id="s-multiple-object-mixins"> <span id="multiple-object-mixins"></span><h1>Multiple object mixins<a class="headerlink" href="#multiple-object-mixins" title="Permalink to this headline">¶</a></h1> <div class="section" id="s-multipleobjectmixin"> <span id="multipleobjectmixin"></span><h2>MultipleObjectMixin<a class="headerlink" href="#multipleobjectmixin" title="Permalink to this headline">¶</a></h2> <dl class="class"> <dt id="django.views.generic.list.MultipleObjectMixin"> <em class="property">class </em><tt class="descclassname">django.views.generic.list.</tt><tt class="descname">MultipleObjectMixin</tt><a class="headerlink" href="#django.views.generic.list.MultipleObjectMixin" title="Permalink to this definition">¶</a></dt> <dd><p>A mixin that can be used to display a list of objects.</p> <p>If <tt class="docutils literal"><span class="pre">paginate_by</span></tt> is specified, Django will paginate the results returned by this. You can specify the page number in the URL in one of two ways:</p> <ul> <li><p class="first">Use the <tt class="docutils literal"><span class="pre">page</span></tt> parameter in the URLconf. For example, this is what your URLconf might look like:</p> <div class="highlight-python"><div class="highlight"><pre><span class="p">(</span><span class="s">r'^objects/page(?P<page>[0-9]+)/$'</span><span class="p">,</span> <span class="n">PaginatedView</span><span class="o">.</span><span class="n">as_view</span><span class="p">())</span> </pre></div> </div> </li> <li><p class="first">Pass the page number via the <tt class="docutils literal"><span class="pre">page</span></tt> query-string parameter. For example, a URL would look like this:</p> <div class="highlight-python"><pre>/objects/?page=3</pre> </div> </li> </ul> <p>These values and lists are 1-based, not 0-based, so the first page would be represented as page <tt class="docutils literal"><span class="pre">1</span></tt>.</p> <p>For more on pagination, read the <a class="reference internal" href="../../topics/pagination.html"><em>pagination documentation</em></a>.</p> <p>As a special case, you are also permitted to use <tt class="docutils literal"><span class="pre">last</span></tt> as a value for <tt class="docutils literal"><span class="pre">page</span></tt>:</p> <div class="highlight-python"><pre>/objects/?page=last</pre> </div> <p>This allows you to access the final page of results without first having to determine how many pages there are.</p> <p>Note that <tt class="docutils literal"><span class="pre">page</span></tt> <em>must</em> be either a valid page number or the value <tt class="docutils literal"><span class="pre">last</span></tt>; any other value for <tt class="docutils literal"><span class="pre">page</span></tt> will result in a 404 error.</p> <p><strong>Extends</strong></p> <ul class="simple"> <li><a class="reference internal" href="mixins-simple.html#django.views.generic.base.ContextMixin" title="django.views.generic.base.ContextMixin"><tt class="xref py py-class docutils literal"><span class="pre">django.views.generic.base.ContextMixin</span></tt></a></li> </ul> <p><strong>Methods and Attributes</strong></p> <dl class="attribute"> <dt id="django.views.generic.list.MultipleObjectMixin.allow_empty"> <tt class="descname">allow_empty</tt><a class="headerlink" href="#django.views.generic.list.MultipleObjectMixin.allow_empty" title="Permalink to this definition">¶</a></dt> <dd><p>A boolean specifying whether to display the page if no objects are available. If this is <tt class="docutils literal"><span class="pre">False</span></tt> and no objects are available, the view will raise a 404 instead of displaying an empty page. By default, this is <tt class="docutils literal"><span class="pre">True</span></tt>.</p> </dd></dl> <dl class="attribute"> <dt id="django.views.generic.list.MultipleObjectMixin.model"> <tt class="descname">model</tt><a class="headerlink" href="#django.views.generic.list.MultipleObjectMixin.model" title="Permalink to this definition">¶</a></dt> <dd><p>The model that this view will display data for. Specifying <tt class="docutils literal"><span class="pre">model</span> <span class="pre">=</span> <span class="pre">Foo</span></tt> is effectively the same as specifying <tt class="docutils literal"><span class="pre">queryset</span> <span class="pre">=</span> <span class="pre">Foo.objects.all()</span></tt>.</p> </dd></dl> <dl class="attribute"> <dt id="django.views.generic.list.MultipleObjectMixin.queryset"> <tt class="descname">queryset</tt><a class="headerlink" href="#django.views.generic.list.MultipleObjectMixin.queryset" title="Permalink to this definition">¶</a></dt> <dd><p>A <tt class="docutils literal"><span class="pre">QuerySet</span></tt> that represents the objects. If provided, the value of <tt class="docutils literal"><span class="pre">queryset</span></tt> supersedes the value provided for <a class="reference internal" href="#django.views.generic.list.MultipleObjectMixin.model" title="django.views.generic.list.MultipleObjectMixin.model"><tt class="xref py py-attr docutils literal"><span class="pre">model</span></tt></a>.</p> </dd></dl> <dl class="attribute"> <dt id="django.views.generic.list.MultipleObjectMixin.paginate_by"> <tt class="descname">paginate_by</tt><a class="headerlink" href="#django.views.generic.list.MultipleObjectMixin.paginate_by" title="Permalink to this definition">¶</a></dt> <dd><p>An integer specifying how many objects should be displayed per page. If this is given, the view will paginate objects with <tt class="docutils literal"><span class="pre">paginate_by</span></tt> objects per page. The view will expect either a <tt class="docutils literal"><span class="pre">page</span></tt> query string parameter (via <tt class="docutils literal"><span class="pre">request.GET</span></tt>) or a <tt class="docutils literal"><span class="pre">page</span></tt> variable specified in the URLconf.</p> </dd></dl> <dl class="attribute"> <dt id="django.views.generic.list.MultipleObjectMixin.page_kwarg"> <tt class="descname">page_kwarg</tt><a class="headerlink" href="#django.views.generic.list.MultipleObjectMixin.page_kwarg" title="Permalink to this definition">¶</a></dt> <dd><div class="versionadded"> <span class="title">New in Django 1.5.</span> </div> <p>A string specifying the name to use for the page parameter. The view will expect this prameter to be available either as a query string parameter (via <tt class="docutils literal"><span class="pre">request.GET</span></tt>) or as a kwarg variable specified in the URLconf. Defaults to <tt class="docutils literal"><span class="pre">page</span></tt>.</p> </dd></dl> <dl class="attribute"> <dt id="django.views.generic.list.MultipleObjectMixin.paginator_class"> <tt class="descname">paginator_class</tt><a class="headerlink" href="#django.views.generic.list.MultipleObjectMixin.paginator_class" title="Permalink to this definition">¶</a></dt> <dd><p>The paginator class to be used for pagination. By default, <a class="reference internal" href="../../topics/pagination.html#django.core.paginator.Paginator" title="django.core.paginator.Paginator"><tt class="xref py py-class docutils literal"><span class="pre">django.core.paginator.Paginator</span></tt></a> is used. If the custom paginator class doesn’t have the same constructor interface as <a class="reference internal" href="../../topics/pagination.html#django.core.paginator.Paginator" title="django.core.paginator.Paginator"><tt class="xref py py-class docutils literal"><span class="pre">django.core.paginator.Paginator</span></tt></a>, you will also need to provide an implementation for <a class="reference internal" href="#django.views.generic.list.MultipleObjectMixin.get_paginator" title="django.views.generic.list.MultipleObjectMixin.get_paginator"><tt class="xref py py-meth docutils literal"><span class="pre">get_paginator()</span></tt></a>.</p> </dd></dl> <dl class="attribute"> <dt id="django.views.generic.list.MultipleObjectMixin.context_object_name"> <tt class="descname">context_object_name</tt><a class="headerlink" href="#django.views.generic.list.MultipleObjectMixin.context_object_name" title="Permalink to this definition">¶</a></dt> <dd><p>Designates the name of the variable to use in the context.</p> </dd></dl> <dl class="method"> <dt id="django.views.generic.list.MultipleObjectMixin.get_queryset"> <tt class="descname">get_queryset</tt>()<a class="headerlink" href="#django.views.generic.list.MultipleObjectMixin.get_queryset" title="Permalink to this definition">¶</a></dt> <dd><p>Get the list of items for this view. This must be an iterable and may be a queryset (in which queryset-specific behavior will be enabled).</p> </dd></dl> <dl class="method"> <dt id="django.views.generic.list.MultipleObjectMixin.paginate_queryset"> <tt class="descname">paginate_queryset</tt>(<em>queryset</em>, <em>page_size</em>)<a class="headerlink" href="#django.views.generic.list.MultipleObjectMixin.paginate_queryset" title="Permalink to this definition">¶</a></dt> <dd><p>Returns a 4-tuple containing (<tt class="docutils literal"><span class="pre">paginator</span></tt>, <tt class="docutils literal"><span class="pre">page</span></tt>, <tt class="docutils literal"><span class="pre">object_list</span></tt>, <tt class="docutils literal"><span class="pre">is_paginated</span></tt>).</p> <p>Constructed by paginating <tt class="docutils literal"><span class="pre">queryset</span></tt> into pages of size <tt class="docutils literal"><span class="pre">page_size</span></tt>. If the request contains a <tt class="docutils literal"><span class="pre">page</span></tt> argument, either as a captured URL argument or as a GET argument, <tt class="docutils literal"><span class="pre">object_list</span></tt> will correspond to the objects from that page.</p> </dd></dl> <dl class="method"> <dt id="django.views.generic.list.MultipleObjectMixin.get_paginate_by"> <tt class="descname">get_paginate_by</tt>(<em>queryset</em>)<a class="headerlink" href="#django.views.generic.list.MultipleObjectMixin.get_paginate_by" title="Permalink to this definition">¶</a></dt> <dd><p>Returns the number of items to paginate by, or <tt class="docutils literal"><span class="pre">None</span></tt> for no pagination. By default this simply returns the value of <a class="reference internal" href="#django.views.generic.list.MultipleObjectMixin.paginate_by" title="django.views.generic.list.MultipleObjectMixin.paginate_by"><tt class="xref py py-attr docutils literal"><span class="pre">paginate_by</span></tt></a>.</p> </dd></dl> <dl class="method"> <dt id="django.views.generic.list.MultipleObjectMixin.get_paginator"> <tt class="descname">get_paginator</tt>(<em>queryset</em>, <em>per_page</em>, <em>orphans=0</em>, <em>allow_empty_first_page=True</em>)<a class="headerlink" href="#django.views.generic.list.MultipleObjectMixin.get_paginator" title="Permalink to this definition">¶</a></dt> <dd><p>Returns an instance of the paginator to use for this view. By default, instantiates an instance of <a class="reference internal" href="#django.views.generic.list.MultipleObjectMixin.paginator_class" title="django.views.generic.list.MultipleObjectMixin.paginator_class"><tt class="xref py py-attr docutils literal"><span class="pre">paginator_class</span></tt></a>.</p> </dd></dl> <dl class="method"> <dt id="django.views.generic.list.MultipleObjectMixin.get_allow_empty"> <tt class="descname">get_allow_empty</tt>()<a class="headerlink" href="#django.views.generic.list.MultipleObjectMixin.get_allow_empty" title="Permalink to this definition">¶</a></dt> <dd><p>Return a boolean specifying whether to display the page if no objects are available. If this method returns <tt class="docutils literal"><span class="pre">False</span></tt> and no objects are available, the view will raise a 404 instead of displaying an empty page. By default, this is <tt class="docutils literal"><span class="pre">True</span></tt>.</p> </dd></dl> <dl class="method"> <dt id="django.views.generic.list.MultipleObjectMixin.get_context_object_name"> <tt class="descname">get_context_object_name</tt>(<em>object_list</em>)<a class="headerlink" href="#django.views.generic.list.MultipleObjectMixin.get_context_object_name" title="Permalink to this definition">¶</a></dt> <dd><p>Return the context variable name that will be used to contain the list of data that this view is manipulating. If <tt class="docutils literal"><span class="pre">object_list</span></tt> is a queryset of Django objects and <a class="reference internal" href="#django.views.generic.list.MultipleObjectMixin.context_object_name" title="django.views.generic.list.MultipleObjectMixin.context_object_name"><tt class="xref py py-attr docutils literal"><span class="pre">context_object_name</span></tt></a> is not set, the context name will be the <tt class="docutils literal"><span class="pre">object_name</span></tt> of the model that the queryset is composed from, with postfix <tt class="docutils literal"><span class="pre">'_list'</span></tt> appended. For example, the model <tt class="docutils literal"><span class="pre">Article</span></tt> would have a context object named <tt class="docutils literal"><span class="pre">article_list</span></tt>.</p> </dd></dl> <dl class="method"> <dt id="django.views.generic.list.MultipleObjectMixin.get_context_data"> <tt class="descname">get_context_data</tt>(<em>**kwargs</em>)<a class="headerlink" href="#django.views.generic.list.MultipleObjectMixin.get_context_data" title="Permalink to this definition">¶</a></dt> <dd><p>Returns context data for displaying the list of objects.</p> </dd></dl> <p><strong>Context</strong></p> <ul class="simple"> <li><tt class="docutils literal"><span class="pre">object_list</span></tt>: The list of objects that this view is displaying. If <tt class="docutils literal"><span class="pre">context_object_name</span></tt> is specified, that variable will also be set in the context, with the same value as <tt class="docutils literal"><span class="pre">object_list</span></tt>.</li> <li><tt class="docutils literal"><span class="pre">is_paginated</span></tt>: A boolean representing whether the results are paginated. Specifically, this is set to <tt class="docutils literal"><span class="pre">False</span></tt> if no page size has been specified, or if the available objects do not span multiple pages.</li> <li><tt class="docutils literal"><span class="pre">paginator</span></tt>: An instance of <a class="reference internal" href="../../topics/pagination.html#django.core.paginator.Paginator" title="django.core.paginator.Paginator"><tt class="xref py py-class docutils literal"><span class="pre">django.core.paginator.Paginator</span></tt></a>. If the page is not paginated, this context variable will be <tt class="docutils literal"><span class="pre">None</span></tt>.</li> <li><tt class="docutils literal"><span class="pre">page_obj</span></tt>: An instance of <a class="reference internal" href="../../topics/pagination.html#django.core.paginator.Page" title="django.core.paginator.Page"><tt class="xref py py-class docutils literal"><span class="pre">django.core.paginator.Page</span></tt></a>. If the page is not paginated, this context variable will be <tt class="docutils literal"><span class="pre">None</span></tt>.</li> </ul> </dd></dl> </div> <div class="section" id="s-multipleobjecttemplateresponsemixin"> <span id="multipleobjecttemplateresponsemixin"></span><h2>MultipleObjectTemplateResponseMixin<a class="headerlink" href="#multipleobjecttemplateresponsemixin" title="Permalink to this headline">¶</a></h2> <dl class="class"> <dt id="django.views.generic.list.MultipleObjectTemplateResponseMixin"> <em class="property">class </em><tt class="descclassname">django.views.generic.list.</tt><tt class="descname">MultipleObjectTemplateResponseMixin</tt><a class="headerlink" href="#django.views.generic.list.MultipleObjectTemplateResponseMixin" title="Permalink to this definition">¶</a></dt> <dd><p>A mixin class that performs template-based response rendering for views that operate upon a list of object instances. Requires that the view it is mixed with provides <tt class="docutils literal"><span class="pre">self.object_list</span></tt>, the list of object instances that the view is operating on. <tt class="docutils literal"><span class="pre">self.object_list</span></tt> may be, but is not required to be, a <a class="reference internal" href="../models/querysets.html#django.db.models.query.QuerySet" title="django.db.models.query.QuerySet"><tt class="xref py py-class docutils literal"><span class="pre">QuerySet</span></tt></a>.</p> <p><strong>Extends</strong></p> <ul class="simple"> <li><a class="reference internal" href="mixins-simple.html#django.views.generic.base.TemplateResponseMixin" title="django.views.generic.base.TemplateResponseMixin"><tt class="xref py py-class docutils literal"><span class="pre">TemplateResponseMixin</span></tt></a></li> </ul> <p><strong>Methods and Attributes</strong></p> <dl class="attribute"> <dt id="django.views.generic.list.MultipleObjectTemplateResponseMixin.template_name_suffix"> <tt class="descname">template_name_suffix</tt><a class="headerlink" href="#django.views.generic.list.MultipleObjectTemplateResponseMixin.template_name_suffix" title="Permalink to this definition">¶</a></dt> <dd><p>The suffix to append to the auto-generated candidate template name. Default suffix is <tt class="docutils literal"><span class="pre">_list</span></tt>.</p> </dd></dl> <dl class="method"> <dt id="django.views.generic.list.MultipleObjectTemplateResponseMixin.get_template_names"> <tt class="descname">get_template_names</tt>()<a class="headerlink" href="#django.views.generic.list.MultipleObjectTemplateResponseMixin.get_template_names" title="Permalink to this definition">¶</a></dt> <dd><p>Returns a list of candidate template names. Returns the following list:</p> <ul class="simple"> <li>the value of <tt class="docutils literal"><span class="pre">template_name</span></tt> on the view (if provided)</li> <li><tt class="docutils literal"><span class="pre"><app_label>/<object_name><template_name_suffix>.html</span></tt></li> </ul> </dd></dl> </dd></dl> </div> </div> </div> </div> </div> <div class="yui-b" id="sidebar"> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h3><a href="../../contents.html">Table Of Contents</a></h3> <ul> <li><a class="reference internal" href="#">Multiple object mixins</a><ul> <li><a class="reference internal" href="#multipleobjectmixin">MultipleObjectMixin</a></li> <li><a class="reference internal" href="#multipleobjecttemplateresponsemixin">MultipleObjectTemplateResponseMixin</a></li> </ul> </li> </ul> <h3>Browse</h3> <ul> <li>Prev: <a href="mixins-single-object.html">Single object mixins</a></li> <li>Next: <a href="mixins-editing.html">Editing mixins</a></li> </ul> <h3>You are here:</h3> <ul> <li> <a href="../../index.html">Django 1.5.9 documentation</a> <ul><li><a href="../index.html">API Reference</a> <ul><li><a href="index.html">Class-based views</a> <ul><li><a href="mixins.html">Class-based views mixins</a> <ul><li>Multiple object mixins</li></ul> </li></ul></li></ul></li></ul> </li> </ul> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../../_sources/ref/class-based-views/mixins-multiple-object.txt" rel="nofollow">Show Source</a></li> </ul> <div id="searchbox" style="display: none"> <h3>Quick search</h3> <form class="search" action="../../search.html" method="get"> <input type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> <p class="searchtip" style="font-size: 90%"> Enter search terms or a module, class or function name. </p> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <h3>Last update:</h3> <p class="topless">Aug 21, 2014</p> </div> </div> <div id="ft"> <div class="nav"> « <a href="mixins-single-object.html" title="Single object mixins">previous</a> | <a href="../index.html" title="API Reference" accesskey="U">up</a> | <a href="mixins-editing.html" title="Editing mixins">next</a> »</div> </div> </div> <div class="clearer"></div> </div> </body> </html>