<!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>Generic display views — 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" href="index.html" /> <link rel="next" title="Generic editing views" href="generic-editing.html" /> <link rel="prev" title="Base views" href="base.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="base.html" title="Base views">previous</a> | <a href="../index.html" title="API Reference" accesskey="U">up</a> | <a href="generic-editing.html" title="Generic editing views">next</a> »</div> </div> <div id="bd"> <div id="yui-main"> <div class="yui-b"> <div class="yui-g" id="ref-class-based-views-generic-display"> <div class="section" id="s-generic-display-views"> <span id="generic-display-views"></span><h1>Generic display views<a class="headerlink" href="#generic-display-views" title="Permalink to this headline">¶</a></h1> <p>The two following generic class-based views are designed to display data. On many projects they are typically the most commonly used views.</p> <div class="section" id="s-detailview"> <span id="detailview"></span><h2>DetailView<a class="headerlink" href="#detailview" title="Permalink to this headline">¶</a></h2> <dl class="class"> <dt id="django.views.generic.detail.DetailView"> <em class="property">class </em><tt class="descclassname">django.views.generic.detail.</tt><tt class="descname">DetailView</tt><a class="headerlink" href="#django.views.generic.detail.DetailView" title="Permalink to this definition">¶</a></dt> <dd><p>While this view is executing, <tt class="docutils literal"><span class="pre">self.object</span></tt> will contain the object that the view is operating upon.</p> <p><strong>Ancestors (MRO)</strong></p> <p>This view inherits methods and attributes from the following views:</p> <ul class="simple"> <li><a class="reference internal" href="mixins-single-object.html#django.views.generic.detail.SingleObjectTemplateResponseMixin" title="django.views.generic.detail.SingleObjectTemplateResponseMixin"><tt class="xref py py-class docutils literal"><span class="pre">django.views.generic.detail.SingleObjectTemplateResponseMixin</span></tt></a></li> <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">django.views.generic.base.TemplateResponseMixin</span></tt></a></li> <li><tt class="docutils literal"><span class="pre">django.views.generic.detail.BaseDetailView</span></tt></li> <li><a class="reference internal" href="mixins-single-object.html#django.views.generic.detail.SingleObjectMixin" title="django.views.generic.detail.SingleObjectMixin"><tt class="xref py py-class docutils literal"><span class="pre">django.views.generic.detail.SingleObjectMixin</span></tt></a></li> <li><a class="reference internal" href="base.html#django.views.generic.base.View" title="django.views.generic.base.View"><tt class="xref py py-class docutils literal"><span class="pre">django.views.generic.base.View</span></tt></a></li> </ul> <p><strong>Method Flowchart</strong></p> <ol class="arabic simple"> <li><a class="reference internal" href="base.html#django.views.generic.base.View.dispatch" title="django.views.generic.base.View.dispatch"><tt class="xref py py-meth docutils literal"><span class="pre">dispatch()</span></tt></a></li> <li><a class="reference internal" href="base.html#django.views.generic.base.View.http_method_not_allowed" title="django.views.generic.base.View.http_method_not_allowed"><tt class="xref py py-meth docutils literal"><span class="pre">http_method_not_allowed()</span></tt></a></li> <li><a class="reference internal" href="mixins-simple.html#django.views.generic.base.TemplateResponseMixin.get_template_names" title="django.views.generic.base.TemplateResponseMixin.get_template_names"><tt class="xref py py-meth docutils literal"><span class="pre">get_template_names()</span></tt></a></li> <li><a class="reference internal" href="mixins-single-object.html#django.views.generic.detail.SingleObjectMixin.get_slug_field" title="django.views.generic.detail.SingleObjectMixin.get_slug_field"><tt class="xref py py-meth docutils literal"><span class="pre">get_slug_field()</span></tt></a></li> <li><a class="reference internal" href="mixins-single-object.html#django.views.generic.detail.SingleObjectMixin.get_queryset" title="django.views.generic.detail.SingleObjectMixin.get_queryset"><tt class="xref py py-meth docutils literal"><span class="pre">get_queryset()</span></tt></a></li> <li><a class="reference internal" href="mixins-single-object.html#django.views.generic.detail.SingleObjectMixin.get_object" title="django.views.generic.detail.SingleObjectMixin.get_object"><tt class="xref py py-meth docutils literal"><span class="pre">get_object()</span></tt></a></li> <li><a class="reference internal" href="mixins-single-object.html#django.views.generic.detail.SingleObjectMixin.get_context_object_name" title="django.views.generic.detail.SingleObjectMixin.get_context_object_name"><tt class="xref py py-meth docutils literal"><span class="pre">get_context_object_name()</span></tt></a></li> <li><a class="reference internal" href="mixins-single-object.html#django.views.generic.detail.SingleObjectMixin.get_context_data" title="django.views.generic.detail.SingleObjectMixin.get_context_data"><tt class="xref py py-meth docutils literal"><span class="pre">get_context_data()</span></tt></a></li> <li><tt class="docutils literal"><span class="pre">get()</span></tt></li> <li><a class="reference internal" href="mixins-simple.html#django.views.generic.base.TemplateResponseMixin.render_to_response" title="django.views.generic.base.TemplateResponseMixin.render_to_response"><tt class="xref py py-meth docutils literal"><span class="pre">render_to_response()</span></tt></a></li> </ol> <p><strong>Example views.py</strong>:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.views.generic.detail</span> <span class="kn">import</span> <span class="n">DetailView</span> <span class="kn">from</span> <span class="nn">django.utils</span> <span class="kn">import</span> <span class="n">timezone</span> <span class="kn">from</span> <span class="nn">articles.models</span> <span class="kn">import</span> <span class="n">Article</span> <span class="k">class</span> <span class="nc">ArticleDetailView</span><span class="p">(</span><span class="n">DetailView</span><span class="p">):</span> <span class="n">model</span> <span class="o">=</span> <span class="n">Article</span> <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><span class="n">kwargs</span><span class="p">):</span> <span class="n">context</span> <span class="o">=</span> <span class="nb">super</span><span class="p">(</span><span class="n">ArticleDetailView</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">get_context_data</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span> <span class="n">context</span><span class="p">[</span><span class="s">'now'</span><span class="p">]</span> <span class="o">=</span> <span class="n">timezone</span><span class="o">.</span><span class="n">now</span><span class="p">()</span> <span class="k">return</span> <span class="n">context</span> </pre></div> </div> <p><strong>Example urls.py</strong>:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="kn">import</span> <span class="n">patterns</span><span class="p">,</span> <span class="n">url</span> <span class="kn">from</span> <span class="nn">article.views</span> <span class="kn">import</span> <span class="n">ArticleDetailView</span> <span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">''</span><span class="p">,</span> <span class="n">url</span><span class="p">(</span><span class="s">r'^(?P<slug>[-_\w]+)/$'</span><span class="p">,</span> <span class="n">ArticleDetailView</span><span class="o">.</span><span class="n">as_view</span><span class="p">(),</span> <span class="n">name</span><span class="o">=</span><span class="s">'article-detail'</span><span class="p">),</span> <span class="p">)</span> </pre></div> </div> </dd></dl> </div> <div class="section" id="s-listview"> <span id="listview"></span><h2>ListView<a class="headerlink" href="#listview" title="Permalink to this headline">¶</a></h2> <dl class="class"> <dt id="django.views.generic.list.ListView"> <em class="property">class </em><tt class="descclassname">django.views.generic.list.</tt><tt class="descname">ListView</tt><a class="headerlink" href="#django.views.generic.list.ListView" title="Permalink to this definition">¶</a></dt> <dd><p>A page representing a list of objects.</p> <p>While this view is executing, <tt class="docutils literal"><span class="pre">self.object_list</span></tt> will contain the list of objects (usually, but not necessarily a queryset) that the view is operating upon.</p> <p><strong>Ancestors (MRO)</strong></p> <p>This view inherits methods and attributes from the following views:</p> <ul class="simple"> <li><a class="reference internal" href="mixins-multiple-object.html#django.views.generic.list.MultipleObjectTemplateResponseMixin" title="django.views.generic.list.MultipleObjectTemplateResponseMixin"><tt class="xref py py-class docutils literal"><span class="pre">django.views.generic.list.MultipleObjectTemplateResponseMixin</span></tt></a></li> <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">django.views.generic.base.TemplateResponseMixin</span></tt></a></li> <li><a class="reference internal" href="#django.views.generic.list.BaseListView" title="django.views.generic.list.BaseListView"><tt class="xref py py-class docutils literal"><span class="pre">django.views.generic.list.BaseListView</span></tt></a></li> <li><a class="reference internal" href="mixins-multiple-object.html#django.views.generic.list.MultipleObjectMixin" title="django.views.generic.list.MultipleObjectMixin"><tt class="xref py py-class docutils literal"><span class="pre">django.views.generic.list.MultipleObjectMixin</span></tt></a></li> <li><a class="reference internal" href="base.html#django.views.generic.base.View" title="django.views.generic.base.View"><tt class="xref py py-class docutils literal"><span class="pre">django.views.generic.base.View</span></tt></a></li> </ul> <p><strong>Method Flowchart</strong></p> <ol class="arabic simple"> <li><a class="reference internal" href="base.html#django.views.generic.base.View.dispatch" title="django.views.generic.base.View.dispatch"><tt class="xref py py-meth docutils literal"><span class="pre">dispatch()</span></tt></a></li> <li><a class="reference internal" href="base.html#django.views.generic.base.View.http_method_not_allowed" title="django.views.generic.base.View.http_method_not_allowed"><tt class="xref py py-meth docutils literal"><span class="pre">http_method_not_allowed()</span></tt></a></li> <li><a class="reference internal" href="mixins-simple.html#django.views.generic.base.TemplateResponseMixin.get_template_names" title="django.views.generic.base.TemplateResponseMixin.get_template_names"><tt class="xref py py-meth docutils literal"><span class="pre">get_template_names()</span></tt></a></li> <li><a class="reference internal" href="mixins-multiple-object.html#django.views.generic.list.MultipleObjectMixin.get_queryset" title="django.views.generic.list.MultipleObjectMixin.get_queryset"><tt class="xref py py-meth docutils literal"><span class="pre">get_queryset()</span></tt></a></li> <li><a class="reference internal" href="mixins-multiple-object.html#django.views.generic.list.MultipleObjectMixin.get_context_object_name" title="django.views.generic.list.MultipleObjectMixin.get_context_object_name"><tt class="xref py py-meth docutils literal"><span class="pre">get_context_object_name()</span></tt></a></li> <li><a class="reference internal" href="mixins-multiple-object.html#django.views.generic.list.MultipleObjectMixin.get_context_data" title="django.views.generic.list.MultipleObjectMixin.get_context_data"><tt class="xref py py-meth docutils literal"><span class="pre">get_context_data()</span></tt></a></li> <li><tt class="docutils literal"><span class="pre">get()</span></tt></li> <li><a class="reference internal" href="mixins-simple.html#django.views.generic.base.TemplateResponseMixin.render_to_response" title="django.views.generic.base.TemplateResponseMixin.render_to_response"><tt class="xref py py-meth docutils literal"><span class="pre">render_to_response()</span></tt></a></li> </ol> <p><strong>Example views.py</strong>:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.views.generic.list</span> <span class="kn">import</span> <span class="n">ListView</span> <span class="kn">from</span> <span class="nn">django.utils</span> <span class="kn">import</span> <span class="n">timezone</span> <span class="kn">from</span> <span class="nn">articles.models</span> <span class="kn">import</span> <span class="n">Article</span> <span class="k">class</span> <span class="nc">ArticleListView</span><span class="p">(</span><span class="n">ListView</span><span class="p">):</span> <span class="n">model</span> <span class="o">=</span> <span class="n">Article</span> <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><span class="n">kwargs</span><span class="p">):</span> <span class="n">context</span> <span class="o">=</span> <span class="nb">super</span><span class="p">(</span><span class="n">ArticleListView</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">get_context_data</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span> <span class="n">context</span><span class="p">[</span><span class="s">'now'</span><span class="p">]</span> <span class="o">=</span> <span class="n">timezone</span><span class="o">.</span><span class="n">now</span><span class="p">()</span> <span class="k">return</span> <span class="n">context</span> </pre></div> </div> <p><strong>Example urls.py</strong>:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="kn">import</span> <span class="n">patterns</span><span class="p">,</span> <span class="n">url</span> <span class="kn">from</span> <span class="nn">article.views</span> <span class="kn">import</span> <span class="n">ArticleListView</span> <span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">''</span><span class="p">,</span> <span class="n">url</span><span class="p">(</span><span class="s">r'^$'</span><span class="p">,</span> <span class="n">ArticleListView</span><span class="o">.</span><span class="n">as_view</span><span class="p">(),</span> <span class="n">name</span><span class="o">=</span><span class="s">'article-list'</span><span class="p">),</span> <span class="p">)</span> </pre></div> </div> </dd></dl> <dl class="class"> <dt id="django.views.generic.list.BaseListView"> <em class="property">class </em><tt class="descclassname">django.views.generic.list.</tt><tt class="descname">BaseListView</tt><a class="headerlink" href="#django.views.generic.list.BaseListView" title="Permalink to this definition">¶</a></dt> <dd><p>A base view for displaying a list of objects. It is not intended to be used directly, but rather as a parent class of the <a class="reference internal" href="#django.views.generic.list.ListView" title="django.views.generic.list.ListView"><tt class="xref py py-class docutils literal"><span class="pre">django.views.generic.list.ListView</span></tt></a> or other views representing lists of objects.</p> <p><strong>Ancestors (MRO)</strong></p> <p>This view inherits methods and attributes from the following views:</p> <ul class="simple"> <li><a class="reference internal" href="mixins-multiple-object.html#django.views.generic.list.MultipleObjectMixin" title="django.views.generic.list.MultipleObjectMixin"><tt class="xref py py-class docutils literal"><span class="pre">django.views.generic.list.MultipleObjectMixin</span></tt></a></li> <li><a class="reference internal" href="base.html#django.views.generic.base.View" title="django.views.generic.base.View"><tt class="xref py py-class docutils literal"><span class="pre">django.views.generic.base.View</span></tt></a></li> </ul> <p><strong>Methods</strong></p> <dl class="method"> <dt id="django.views.generic.list.BaseListView.get"> <tt class="descname">get</tt>(<em>request</em>, <em>*args</em>, <em>**kwargs</em>)<a class="headerlink" href="#django.views.generic.list.BaseListView.get" title="Permalink to this definition">¶</a></dt> <dd><p>Adds <tt class="docutils literal"><span class="pre">object_list</span></tt> to the context. If <a class="reference internal" href="mixins-multiple-object.html#django.views.generic.list.MultipleObjectMixin.allow_empty" title="django.views.generic.list.MultipleObjectMixin.allow_empty"><tt class="xref py py-attr docutils literal"><span class="pre">allow_empty</span></tt></a> is True then display an empty list. If <a class="reference internal" href="mixins-multiple-object.html#django.views.generic.list.MultipleObjectMixin.allow_empty" title="django.views.generic.list.MultipleObjectMixin.allow_empty"><tt class="xref py py-attr docutils literal"><span class="pre">allow_empty</span></tt></a> is False then raise a 404 error.</p> </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="#">Generic display views</a><ul> <li><a class="reference internal" href="#detailview">DetailView</a></li> <li><a class="reference internal" href="#listview">ListView</a></li> </ul> </li> </ul> <h3>Browse</h3> <ul> <li>Prev: <a href="base.html">Base views</a></li> <li>Next: <a href="generic-editing.html">Generic editing views</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>Generic display views</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/generic-display.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">Jan 15, 2015</p> </div> </div> <div id="ft"> <div class="nav"> « <a href="base.html" title="Base views">previous</a> | <a href="../index.html" title="API Reference" accesskey="U">up</a> | <a href="generic-editing.html" title="Generic editing views">next</a> »</div> </div> </div> <div class="clearer"></div> </div> </body> </html>