<!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" lang=""> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Generic display views — Django 1.8.19 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.8.19', 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="index" title="Index" href="../../genindex.html" /> <link rel="search" title="Search" href="../../search.html" /> <link rel="top" title="Django 1.8.19 documentation" href="../../contents.html" /> <link rel="up" title="Built-in class-based views API" 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 role="document"> <div class="document"> <div id="custom-doc" class="yui-t6"> <div id="hd"> <h1><a href="../../index.html">Django 1.8.19 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><code class="descclassname">django.views.generic.detail.</code><code class="descname">DetailView</code><a class="headerlink" href="#django.views.generic.detail.DetailView" title="Permalink to this definition">¶</a></dt> <dd><p>While this view is executing, <code class="docutils literal"><span class="pre">self.object</span></code> 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"><code class="xref py py-class docutils literal"><span class="pre">django.views.generic.detail.SingleObjectTemplateResponseMixin</span></code></a></li> <li><a class="reference internal" href="mixins-simple.html#django.views.generic.base.TemplateResponseMixin" title="django.views.generic.base.TemplateResponseMixin"><code class="xref py py-class docutils literal"><span class="pre">django.views.generic.base.TemplateResponseMixin</span></code></a></li> <li><code class="docutils literal"><span class="pre">django.views.generic.detail.BaseDetailView</span></code></li> <li><a class="reference internal" href="mixins-single-object.html#django.views.generic.detail.SingleObjectMixin" title="django.views.generic.detail.SingleObjectMixin"><code class="xref py py-class docutils literal"><span class="pre">django.views.generic.detail.SingleObjectMixin</span></code></a></li> <li><a class="reference internal" href="base.html#django.views.generic.base.View" title="django.views.generic.base.View"><code class="xref py py-class docutils literal"><span class="pre">django.views.generic.base.View</span></code></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"><code class="xref py py-meth docutils literal"><span class="pre">dispatch()</span></code></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"><code class="xref py py-meth docutils literal"><span class="pre">http_method_not_allowed()</span></code></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"><code class="xref py py-meth docutils literal"><span class="pre">get_template_names()</span></code></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"><code class="xref py py-meth docutils literal"><span class="pre">get_slug_field()</span></code></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"><code class="xref py py-meth docutils literal"><span class="pre">get_queryset()</span></code></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"><code class="xref py py-meth docutils literal"><span class="pre">get_object()</span></code></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"><code class="xref py py-meth docutils literal"><span class="pre">get_context_object_name()</span></code></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"><code class="xref py py-meth docutils literal"><span class="pre">get_context_data()</span></code></a></li> <li><code class="docutils literal"><span class="pre">get()</span></code></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"><code class="xref py py-meth docutils literal"><span class="pre">render_to_response()</span></code></a></li> </ol> <p><strong>Example myapp/views.py</strong>:</p> <div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.views.generic.detail</span> <span class="k">import</span> <span class="n">DetailView</span> <span class="kn">from</span> <span class="nn">django.utils</span> <span class="k">import</span> <span class="n">timezone</span> <span class="kn">from</span> <span class="nn">articles.models</span> <span class="k">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="s1">'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 myapp/urls.py</strong>:</p> <div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="k">import</span> <span class="n">url</span> <span class="kn">from</span> <span class="nn">article.views</span> <span class="k">import</span> <span class="n">ArticleDetailView</span> <span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span> <span class="n">url</span><span class="p">(</span><span class="s1">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="s1">'article-detail'</span><span class="p">),</span> <span class="p">]</span> </pre></div> </div> <p><strong>Example myapp/article_detail.html</strong>:</p> <div class="highlight-html+django"><div class="highlight"><pre><span></span><span class="p"><</span><span class="nt">h1</span><span class="p">></span><span class="cp">{{</span> <span class="nv">object.headline</span> <span class="cp">}}</span><span class="p"></</span><span class="nt">h1</span><span class="p">></span> <span class="p"><</span><span class="nt">p</span><span class="p">></span><span class="cp">{{</span> <span class="nv">object.content</span> <span class="cp">}}</span><span class="p"></</span><span class="nt">p</span><span class="p">></span> <span class="p"><</span><span class="nt">p</span><span class="p">></span>Reporter: <span class="cp">{{</span> <span class="nv">object.reporter</span> <span class="cp">}}</span><span class="p"></</span><span class="nt">p</span><span class="p">></span> <span class="p"><</span><span class="nt">p</span><span class="p">></span>Published: <span class="cp">{{</span> <span class="nv">object.pub_date</span><span class="o">|</span><span class="nf">date</span> <span class="cp">}}</span><span class="p"></</span><span class="nt">p</span><span class="p">></span> <span class="p"><</span><span class="nt">p</span><span class="p">></span>Date: <span class="cp">{{</span> <span class="nv">now</span><span class="o">|</span><span class="nf">date</span> <span class="cp">}}</span><span class="p"></</span><span class="nt">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><code class="descclassname">django.views.generic.list.</code><code class="descname">ListView</code><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, <code class="docutils literal"><span class="pre">self.object_list</span></code> 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"><code class="xref py py-class docutils literal"><span class="pre">django.views.generic.list.MultipleObjectTemplateResponseMixin</span></code></a></li> <li><a class="reference internal" href="mixins-simple.html#django.views.generic.base.TemplateResponseMixin" title="django.views.generic.base.TemplateResponseMixin"><code class="xref py py-class docutils literal"><span class="pre">django.views.generic.base.TemplateResponseMixin</span></code></a></li> <li><a class="reference internal" href="#django.views.generic.list.BaseListView" title="django.views.generic.list.BaseListView"><code class="xref py py-class docutils literal"><span class="pre">django.views.generic.list.BaseListView</span></code></a></li> <li><a class="reference internal" href="mixins-multiple-object.html#django.views.generic.list.MultipleObjectMixin" title="django.views.generic.list.MultipleObjectMixin"><code class="xref py py-class docutils literal"><span class="pre">django.views.generic.list.MultipleObjectMixin</span></code></a></li> <li><a class="reference internal" href="base.html#django.views.generic.base.View" title="django.views.generic.base.View"><code class="xref py py-class docutils literal"><span class="pre">django.views.generic.base.View</span></code></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"><code class="xref py py-meth docutils literal"><span class="pre">dispatch()</span></code></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"><code class="xref py py-meth docutils literal"><span class="pre">http_method_not_allowed()</span></code></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"><code class="xref py py-meth docutils literal"><span class="pre">get_template_names()</span></code></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"><code class="xref py py-meth docutils literal"><span class="pre">get_queryset()</span></code></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"><code class="xref py py-meth docutils literal"><span class="pre">get_context_object_name()</span></code></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"><code class="xref py py-meth docutils literal"><span class="pre">get_context_data()</span></code></a></li> <li><code class="docutils literal"><span class="pre">get()</span></code></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"><code class="xref py py-meth docutils literal"><span class="pre">render_to_response()</span></code></a></li> </ol> <p><strong>Example views.py</strong>:</p> <div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.views.generic.list</span> <span class="k">import</span> <span class="n">ListView</span> <span class="kn">from</span> <span class="nn">django.utils</span> <span class="k">import</span> <span class="n">timezone</span> <span class="kn">from</span> <span class="nn">articles.models</span> <span class="k">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="s1">'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 myapp/urls.py</strong>:</p> <div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="k">import</span> <span class="n">url</span> <span class="kn">from</span> <span class="nn">article.views</span> <span class="k">import</span> <span class="n">ArticleListView</span> <span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span> <span class="n">url</span><span class="p">(</span><span class="s1">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="s1">'article-list'</span><span class="p">),</span> <span class="p">]</span> </pre></div> </div> <p><strong>Example myapp/article_list.html</strong>:</p> <div class="highlight-html+django"><div class="highlight"><pre><span></span><span class="p"><</span><span class="nt">h1</span><span class="p">></span>Articles<span class="p"></</span><span class="nt">h1</span><span class="p">></span> <span class="p"><</span><span class="nt">ul</span><span class="p">></span> <span class="cp">{%</span> <span class="k">for</span> <span class="nv">article</span> <span class="k">in</span> <span class="nv">object_list</span> <span class="cp">%}</span> <span class="p"><</span><span class="nt">li</span><span class="p">></span><span class="cp">{{</span> <span class="nv">article.pub_date</span><span class="o">|</span><span class="nf">date</span> <span class="cp">}}</span> - <span class="cp">{{</span> <span class="nv">article.headline</span> <span class="cp">}}</span><span class="p"></</span><span class="nt">li</span><span class="p">></span> <span class="cp">{%</span> <span class="k">empty</span> <span class="cp">%}</span> <span class="p"><</span><span class="nt">li</span><span class="p">></span>No articles yet.<span class="p"></</span><span class="nt">li</span><span class="p">></span> <span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span> <span class="p"></</span><span class="nt">ul</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><code class="descclassname">django.views.generic.list.</code><code class="descname">BaseListView</code><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"><code class="xref py py-class docutils literal"><span class="pre">django.views.generic.list.ListView</span></code></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"><code class="xref py py-class docutils literal"><span class="pre">django.views.generic.list.MultipleObjectMixin</span></code></a></li> <li><a class="reference internal" href="base.html#django.views.generic.base.View" title="django.views.generic.base.View"><code class="xref py py-class docutils literal"><span class="pre">django.views.generic.base.View</span></code></a></li> </ul> <p><strong>Methods</strong></p> <dl class="method"> <dt id="django.views.generic.list.BaseListView.get"> <code class="descname">get</code>(<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 <code class="docutils literal"><span class="pre">object_list</span></code> 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"><code class="xref py py-attr docutils literal"><span class="pre">allow_empty</span></code></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"><code class="xref py py-attr docutils literal"><span class="pre">allow_empty</span></code></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" role="navigation" aria-label="main navigation"> <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.8.19 documentation</a> <ul><li><a href="../index.html">API Reference</a> <ul><li><a href="index.html">Built-in class-based views API</a> <ul><li>Generic display views</li></ul> </li></ul></li></ul> </li> </ul> <div role="note" aria-label="source link"> <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> <div id="searchbox" style="display: none" role="search"> <h3>Quick search</h3> <form class="search" action="../../search.html" method="get"> <div><input type="text" name="q" /></div> <div><input type="submit" value="Go" /></div> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <h3>Last update:</h3> <p class="topless">Mar 10, 2018</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>