Sophie

Sophie

distrib > Mageia > 6 > armv7hl > media > core-updates > by-pkgid > 65530c6176058f9b54858c3b4f6385e6 > files > 670

python-django-doc-1.8.19-1.mga6.noarch.rpm

<!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>Writing your first Django app, part 4 &#8212; 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="Getting started" href="index.html" />
    <link rel="next" title="Writing your first Django app, part 5" href="tutorial05.html" />
    <link rel="prev" title="Writing your first Django app, part 3" href="tutorial03.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 = "../ref/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">
    &laquo; <a href="tutorial03.html" title="Writing your first Django app, part 3">previous</a>
     |
    <a href="index.html" title="Getting started" accesskey="U">up</a>
   |
    <a href="tutorial05.html" title="Writing your first Django app, part 5">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="intro-tutorial04">
            
  <div class="section" id="s-writing-your-first-django-app-part-4">
<span id="writing-your-first-django-app-part-4"></span><h1>Writing your first Django app, part 4<a class="headerlink" href="#writing-your-first-django-app-part-4" title="Permalink to this headline">¶</a></h1>
<p>This tutorial begins where <a class="reference internal" href="tutorial03.html"><span class="doc">Tutorial 3</span></a> left off. We&#8217;re
continuing the Web-poll application and will focus on simple form processing and
cutting down our code.</p>
<div class="section" id="s-write-a-simple-form">
<span id="write-a-simple-form"></span><h2>Write a simple form<a class="headerlink" href="#write-a-simple-form" title="Permalink to this headline">¶</a></h2>
<p>Let&#8217;s update our poll detail template (&#8220;polls/detail.html&#8221;) from the last
tutorial, so that the template contains an HTML <code class="docutils literal"><span class="pre">&lt;form&gt;</span></code> element:</p>
<div class="highlight-html+django"><div class="snippet-filename">polls/templates/polls/detail.html</div>
<div class="highlight"><pre><span></span><span class="p">&lt;</span><span class="nt">h1</span><span class="p">&gt;</span><span class="cp">{{</span> <span class="nv">question.question_text</span> <span class="cp">}}</span><span class="p">&lt;/</span><span class="nt">h1</span><span class="p">&gt;</span>

<span class="cp">{%</span> <span class="k">if</span> <span class="nv">error_message</span> <span class="cp">%}</span><span class="p">&lt;</span><span class="nt">p</span><span class="p">&gt;&lt;</span><span class="nt">strong</span><span class="p">&gt;</span><span class="cp">{{</span> <span class="nv">error_message</span> <span class="cp">}}</span><span class="p">&lt;/</span><span class="nt">strong</span><span class="p">&gt;&lt;/</span><span class="nt">p</span><span class="p">&gt;</span><span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>

<span class="p">&lt;</span><span class="nt">form</span> <span class="na">action</span><span class="o">=</span><span class="s">&quot;</span><span class="cp">{%</span> <span class="k">url</span> <span class="s1">&#39;polls:vote&#39;</span> <span class="nv">question.id</span> <span class="cp">%}</span><span class="s">&quot;</span> <span class="na">method</span><span class="o">=</span><span class="s">&quot;post&quot;</span><span class="p">&gt;</span>
<span class="cp">{%</span> <span class="k">csrf_token</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">for</span> <span class="nv">choice</span> <span class="k">in</span> <span class="nv">question.choice_set.all</span> <span class="cp">%}</span>
    <span class="p">&lt;</span><span class="nt">input</span> <span class="na">type</span><span class="o">=</span><span class="s">&quot;radio&quot;</span> <span class="na">name</span><span class="o">=</span><span class="s">&quot;choice&quot;</span> <span class="na">id</span><span class="o">=</span><span class="s">&quot;choice</span><span class="cp">{{</span> <span class="nb">forloop</span><span class="nv">.counter</span> <span class="cp">}}</span><span class="s">&quot;</span> <span class="na">value</span><span class="o">=</span><span class="s">&quot;</span><span class="cp">{{</span> <span class="nv">choice.id</span> <span class="cp">}}</span><span class="s">&quot;</span> <span class="p">/&gt;</span>
    <span class="p">&lt;</span><span class="nt">label</span> <span class="na">for</span><span class="o">=</span><span class="s">&quot;choice</span><span class="cp">{{</span> <span class="nb">forloop</span><span class="nv">.counter</span> <span class="cp">}}</span><span class="s">&quot;</span><span class="p">&gt;</span><span class="cp">{{</span> <span class="nv">choice.choice_text</span> <span class="cp">}}</span><span class="p">&lt;/</span><span class="nt">label</span><span class="p">&gt;&lt;</span><span class="nt">br</span> <span class="p">/&gt;</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
<span class="p">&lt;</span><span class="nt">input</span> <span class="na">type</span><span class="o">=</span><span class="s">&quot;submit&quot;</span> <span class="na">value</span><span class="o">=</span><span class="s">&quot;Vote&quot;</span> <span class="p">/&gt;</span>
<span class="p">&lt;/</span><span class="nt">form</span><span class="p">&gt;</span>
</pre></div>
</div>
<p>A quick rundown:</p>
<ul class="simple">
<li>The above template displays a radio button for each question choice. The
<code class="docutils literal"><span class="pre">value</span></code> of each radio button is the associated question choice&#8217;s ID. The
<code class="docutils literal"><span class="pre">name</span></code> of each radio button is <code class="docutils literal"><span class="pre">&quot;choice&quot;</span></code>. That means, when somebody
selects one of the radio buttons and submits the form, it&#8217;ll send the
POST data <code class="docutils literal"><span class="pre">choice=#</span></code> where # is the ID of the selected choice. This is the
basic concept of HTML forms.</li>
<li>We set the form&#8217;s <code class="docutils literal"><span class="pre">action</span></code> to <code class="docutils literal"><span class="pre">{%</span> <span class="pre">url</span> <span class="pre">'polls:vote'</span> <span class="pre">question.id</span> <span class="pre">%}</span></code>, and we
set <code class="docutils literal"><span class="pre">method=&quot;post&quot;</span></code>. Using <code class="docutils literal"><span class="pre">method=&quot;post&quot;</span></code> (as opposed to
<code class="docutils literal"><span class="pre">method=&quot;get&quot;</span></code>) is very important, because the act of submitting this
form will alter data server-side. Whenever you create a form that alters
data server-side, use <code class="docutils literal"><span class="pre">method=&quot;post&quot;</span></code>. This tip isn&#8217;t specific to
Django; it&#8217;s just good Web development practice.</li>
<li><code class="docutils literal"><span class="pre">forloop.counter</span></code> indicates how many times the <a class="reference internal" href="../ref/templates/builtins.html#std:templatetag-for"><code class="xref std std-ttag docutils literal"><span class="pre">for</span></code></a> tag has gone
through its loop</li>
<li>Since we&#8217;re creating a POST form (which can have the effect of modifying
data), we need to worry about Cross Site Request Forgeries.
Thankfully, you don&#8217;t have to worry too hard, because Django comes with
a very easy-to-use system for protecting against it. In short, all POST
forms that are targeted at internal URLs should use the
<a class="reference internal" href="../ref/templates/builtins.html#std:templatetag-csrf_token"><code class="xref std std-ttag docutils literal"><span class="pre">{%</span> <span class="pre">csrf_token</span> <span class="pre">%}</span></code></a> template tag.</li>
</ul>
<p>Now, let&#8217;s create a Django view that handles the submitted data and does
something with it. Remember, in <a class="reference internal" href="tutorial03.html"><span class="doc">Tutorial 3</span></a>, we
created a URLconf for the polls application that includes this line:</p>
<div class="highlight-default"><div class="snippet-filename">polls/urls.py</div>
<div class="highlight"><pre><span></span><span class="n">url</span><span class="p">(</span><span class="s1">r&#39;^(?P&lt;question_id&gt;[0-9]+)/vote/$&#39;</span><span class="p">,</span> <span class="n">views</span><span class="o">.</span><span class="n">vote</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;vote&#39;</span><span class="p">),</span>
</pre></div>
</div>
<p>We also created a dummy implementation of the <code class="docutils literal"><span class="pre">vote()</span></code> function. Let&#8217;s
create a real version. Add the following to <code class="docutils literal"><span class="pre">polls/views.py</span></code>:</p>
<div class="highlight-default"><div class="snippet-filename">polls/views.py</div>
<div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.shortcuts</span> <span class="k">import</span> <span class="n">get_object_or_404</span><span class="p">,</span> <span class="n">render</span>
<span class="kn">from</span> <span class="nn">django.http</span> <span class="k">import</span> <span class="n">HttpResponseRedirect</span><span class="p">,</span> <span class="n">HttpResponse</span>
<span class="kn">from</span> <span class="nn">django.core.urlresolvers</span> <span class="k">import</span> <span class="n">reverse</span>

<span class="kn">from</span> <span class="nn">.models</span> <span class="k">import</span> <span class="n">Choice</span><span class="p">,</span> <span class="n">Question</span>
<span class="c1"># ...</span>
<span class="k">def</span> <span class="nf">vote</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">question_id</span><span class="p">):</span>
    <span class="n">p</span> <span class="o">=</span> <span class="n">get_object_or_404</span><span class="p">(</span><span class="n">Question</span><span class="p">,</span> <span class="n">pk</span><span class="o">=</span><span class="n">question_id</span><span class="p">)</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">selected_choice</span> <span class="o">=</span> <span class="n">p</span><span class="o">.</span><span class="n">choice_set</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">pk</span><span class="o">=</span><span class="n">request</span><span class="o">.</span><span class="n">POST</span><span class="p">[</span><span class="s1">&#39;choice&#39;</span><span class="p">])</span>
    <span class="k">except</span> <span class="p">(</span><span class="ne">KeyError</span><span class="p">,</span> <span class="n">Choice</span><span class="o">.</span><span class="n">DoesNotExist</span><span class="p">):</span>
        <span class="c1"># Redisplay the question voting form.</span>
        <span class="k">return</span> <span class="n">render</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="s1">&#39;polls/detail.html&#39;</span><span class="p">,</span> <span class="p">{</span>
            <span class="s1">&#39;question&#39;</span><span class="p">:</span> <span class="n">p</span><span class="p">,</span>
            <span class="s1">&#39;error_message&#39;</span><span class="p">:</span> <span class="s2">&quot;You didn&#39;t select a choice.&quot;</span><span class="p">,</span>
        <span class="p">})</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">selected_choice</span><span class="o">.</span><span class="n">votes</span> <span class="o">+=</span> <span class="mi">1</span>
        <span class="n">selected_choice</span><span class="o">.</span><span class="n">save</span><span class="p">()</span>
        <span class="c1"># Always return an HttpResponseRedirect after successfully dealing</span>
        <span class="c1"># with POST data. This prevents data from being posted twice if a</span>
        <span class="c1"># user hits the Back button.</span>
        <span class="k">return</span> <span class="n">HttpResponseRedirect</span><span class="p">(</span><span class="n">reverse</span><span class="p">(</span><span class="s1">&#39;polls:results&#39;</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="n">p</span><span class="o">.</span><span class="n">id</span><span class="p">,)))</span>
</pre></div>
</div>
<p>This code includes a few things we haven&#8217;t covered yet in this tutorial:</p>
<ul>
<li><p class="first"><a class="reference internal" href="../ref/request-response.html#django.http.HttpRequest.POST" title="django.http.HttpRequest.POST"><code class="xref py py-attr docutils literal"><span class="pre">request.POST</span></code></a> is a dictionary-like
object that lets you access submitted data by key name. In this case,
<code class="docutils literal"><span class="pre">request.POST['choice']</span></code> returns the ID of the selected choice, as a
string. <a class="reference internal" href="../ref/request-response.html#django.http.HttpRequest.POST" title="django.http.HttpRequest.POST"><code class="xref py py-attr docutils literal"><span class="pre">request.POST</span></code></a> values are
always strings.</p>
<p>Note that Django also provides <a class="reference internal" href="../ref/request-response.html#django.http.HttpRequest.GET" title="django.http.HttpRequest.GET"><code class="xref py py-attr docutils literal"><span class="pre">request.GET</span></code></a> for accessing GET data in the same way &#8211;
but we&#8217;re explicitly using <a class="reference internal" href="../ref/request-response.html#django.http.HttpRequest.POST" title="django.http.HttpRequest.POST"><code class="xref py py-attr docutils literal"><span class="pre">request.POST</span></code></a> in our code, to ensure that data is only
altered via a POST call.</p>
</li>
<li><p class="first"><code class="docutils literal"><span class="pre">request.POST['choice']</span></code> will raise <code class="xref py py-exc docutils literal"><span class="pre">KeyError</span></code> if
<code class="docutils literal"><span class="pre">choice</span></code> wasn&#8217;t provided in POST data. The above code checks for
<code class="xref py py-exc docutils literal"><span class="pre">KeyError</span></code> and redisplays the question form with an error
message if <code class="docutils literal"><span class="pre">choice</span></code> isn&#8217;t given.</p>
</li>
<li><p class="first">After incrementing the choice count, the code returns an
<a class="reference internal" href="../ref/request-response.html#django.http.HttpResponseRedirect" title="django.http.HttpResponseRedirect"><code class="xref py py-class docutils literal"><span class="pre">HttpResponseRedirect</span></code></a> rather than a normal
<a class="reference internal" href="../ref/request-response.html#django.http.HttpResponse" title="django.http.HttpResponse"><code class="xref py py-class docutils literal"><span class="pre">HttpResponse</span></code></a>.
<a class="reference internal" href="../ref/request-response.html#django.http.HttpResponseRedirect" title="django.http.HttpResponseRedirect"><code class="xref py py-class docutils literal"><span class="pre">HttpResponseRedirect</span></code></a> takes a single argument: the
URL to which the user will be redirected (see the following point for how
we construct the URL in this case).</p>
<p>As the Python comment above points out, you should always return an
<a class="reference internal" href="../ref/request-response.html#django.http.HttpResponseRedirect" title="django.http.HttpResponseRedirect"><code class="xref py py-class docutils literal"><span class="pre">HttpResponseRedirect</span></code></a> after successfully dealing with
POST data. This tip isn&#8217;t specific to Django; it&#8217;s just good Web
development practice.</p>
</li>
<li><p class="first">We are using the <a class="reference internal" href="../ref/urlresolvers.html#django.core.urlresolvers.reverse" title="django.core.urlresolvers.reverse"><code class="xref py py-func docutils literal"><span class="pre">reverse()</span></code></a> function in the
<a class="reference internal" href="../ref/request-response.html#django.http.HttpResponseRedirect" title="django.http.HttpResponseRedirect"><code class="xref py py-class docutils literal"><span class="pre">HttpResponseRedirect</span></code></a> constructor in this example.
This function helps avoid having to hardcode a URL in the view function.
It is given the name of the view that we want to pass control to and the
variable portion of the URL pattern that points to that view. In this
case, using the URLconf we set up in Tutorial 3, this
<a class="reference internal" href="../ref/urlresolvers.html#django.core.urlresolvers.reverse" title="django.core.urlresolvers.reverse"><code class="xref py py-func docutils literal"><span class="pre">reverse()</span></code></a> call will return a string like</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="s1">&#39;/polls/3/results/&#39;</span>
</pre></div>
</div>
<p>... where the <code class="docutils literal"><span class="pre">3</span></code> is the value of <code class="docutils literal"><span class="pre">p.id</span></code>. This redirected URL will
then call the <code class="docutils literal"><span class="pre">'results'</span></code> view to display the final page.</p>
</li>
</ul>
<p>As mentioned in Tutorial 3, <code class="docutils literal"><span class="pre">request</span></code> is a <a class="reference internal" href="../ref/request-response.html#django.http.HttpRequest" title="django.http.HttpRequest"><code class="xref py py-class docutils literal"><span class="pre">HttpRequest</span></code></a>
object. For more on <a class="reference internal" href="../ref/request-response.html#django.http.HttpRequest" title="django.http.HttpRequest"><code class="xref py py-class docutils literal"><span class="pre">HttpRequest</span></code></a> objects, see the
<a class="reference internal" href="../ref/request-response.html"><span class="doc">request and response documentation</span></a>.</p>
<p>After somebody votes in a question, the <code class="docutils literal"><span class="pre">vote()</span></code> view redirects to the results
page for the question. Let&#8217;s write that view:</p>
<div class="highlight-default"><div class="snippet-filename">polls/views.py</div>
<div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.shortcuts</span> <span class="k">import</span> <span class="n">get_object_or_404</span><span class="p">,</span> <span class="n">render</span>


<span class="k">def</span> <span class="nf">results</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">question_id</span><span class="p">):</span>
    <span class="n">question</span> <span class="o">=</span> <span class="n">get_object_or_404</span><span class="p">(</span><span class="n">Question</span><span class="p">,</span> <span class="n">pk</span><span class="o">=</span><span class="n">question_id</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">render</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="s1">&#39;polls/results.html&#39;</span><span class="p">,</span> <span class="p">{</span><span class="s1">&#39;question&#39;</span><span class="p">:</span> <span class="n">question</span><span class="p">})</span>
</pre></div>
</div>
<p>This is almost exactly the same as the <code class="docutils literal"><span class="pre">detail()</span></code> view from <a class="reference internal" href="tutorial03.html"><span class="doc">Tutorial 3</span></a>. The only difference is the template name. We&#8217;ll fix this
redundancy later.</p>
<p>Now, create a <code class="docutils literal"><span class="pre">polls/results.html</span></code> template:</p>
<div class="highlight-html+django"><div class="snippet-filename">polls/templates/polls/results.html</div>
<div class="highlight"><pre><span></span><span class="p">&lt;</span><span class="nt">h1</span><span class="p">&gt;</span><span class="cp">{{</span> <span class="nv">question.question_text</span> <span class="cp">}}</span><span class="p">&lt;/</span><span class="nt">h1</span><span class="p">&gt;</span>

<span class="p">&lt;</span><span class="nt">ul</span><span class="p">&gt;</span>
<span class="cp">{%</span> <span class="k">for</span> <span class="nv">choice</span> <span class="k">in</span> <span class="nv">question.choice_set.all</span> <span class="cp">%}</span>
    <span class="p">&lt;</span><span class="nt">li</span><span class="p">&gt;</span><span class="cp">{{</span> <span class="nv">choice.choice_text</span> <span class="cp">}}</span> -- <span class="cp">{{</span> <span class="nv">choice.votes</span> <span class="cp">}}</span> vote<span class="cp">{{</span> <span class="nv">choice.votes</span><span class="o">|</span><span class="nf">pluralize</span> <span class="cp">}}</span><span class="p">&lt;/</span><span class="nt">li</span><span class="p">&gt;</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
<span class="p">&lt;/</span><span class="nt">ul</span><span class="p">&gt;</span>

<span class="p">&lt;</span><span class="nt">a</span> <span class="na">href</span><span class="o">=</span><span class="s">&quot;</span><span class="cp">{%</span> <span class="k">url</span> <span class="s1">&#39;polls:detail&#39;</span> <span class="nv">question.id</span> <span class="cp">%}</span><span class="s">&quot;</span><span class="p">&gt;</span>Vote again?<span class="p">&lt;/</span><span class="nt">a</span><span class="p">&gt;</span>
</pre></div>
</div>
<p>Now, go to <code class="docutils literal"><span class="pre">/polls/1/</span></code> in your browser and vote in the question. You should see a
results page that gets updated each time you vote. If you submit the form
without having chosen a choice, you should see the error message.</p>
</div>
<div class="section" id="s-use-generic-views-less-code-is-better">
<span id="use-generic-views-less-code-is-better"></span><h2>Use generic views: Less code is better<a class="headerlink" href="#use-generic-views-less-code-is-better" title="Permalink to this headline">¶</a></h2>
<p>The <code class="docutils literal"><span class="pre">detail()</span></code> (from <a class="reference internal" href="tutorial03.html"><span class="doc">Tutorial 3</span></a>) and <code class="docutils literal"><span class="pre">results()</span></code>
views are very simple &#8211; and, as mentioned above, redundant. The <code class="docutils literal"><span class="pre">index()</span></code>
view (also from Tutorial 3), which displays a list of polls, is similar.</p>
<p>These views represent a common case of basic Web development: getting data from
the database according to a parameter passed in the URL, loading a template and
returning the rendered template. Because this is so common, Django provides a
shortcut, called the &#8220;generic views&#8221; system.</p>
<p>Generic views abstract common patterns to the point where you don&#8217;t even need
to write Python code to write an app.</p>
<p>Let&#8217;s convert our poll app to use the generic views system, so we can delete a
bunch of our own code. We&#8217;ll just have to take a few steps to make the
conversion. We will:</p>
<ol class="arabic simple">
<li>Convert the URLconf.</li>
<li>Delete some of the old, unneeded views.</li>
<li>Introduce new views based on Django&#8217;s generic views.</li>
</ol>
<p>Read on for details.</p>
<div class="admonition-why-the-code-shuffle admonition">
<p class="first admonition-title">Why the code-shuffle?</p>
<p>Generally, when writing a Django app, you&#8217;ll evaluate whether generic views
are a good fit for your problem, and you&#8217;ll use them from the beginning,
rather than refactoring your code halfway through. But this tutorial
intentionally has focused on writing the views &#8220;the hard way&#8221; until now, to
focus on core concepts.</p>
<p class="last">You should know basic math before you start using a calculator.</p>
</div>
<div class="section" id="s-amend-urlconf">
<span id="amend-urlconf"></span><h3>Amend URLconf<a class="headerlink" href="#amend-urlconf" title="Permalink to this headline">¶</a></h3>
<p>First, open the <code class="docutils literal"><span class="pre">polls/urls.py</span></code> URLconf and change it like so:</p>
<div class="highlight-default"><div class="snippet-filename">polls/urls.py</div>
<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">.</span> <span class="k">import</span> <span class="n">views</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&#39;^$&#39;</span><span class="p">,</span> <span class="n">views</span><span class="o">.</span><span class="n">IndexView</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">&#39;index&#39;</span><span class="p">),</span>
    <span class="n">url</span><span class="p">(</span><span class="s1">r&#39;^(?P&lt;pk&gt;[0-9]+)/$&#39;</span><span class="p">,</span> <span class="n">views</span><span class="o">.</span><span class="n">DetailView</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">&#39;detail&#39;</span><span class="p">),</span>
    <span class="n">url</span><span class="p">(</span><span class="s1">r&#39;^(?P&lt;pk&gt;[0-9]+)/results/$&#39;</span><span class="p">,</span> <span class="n">views</span><span class="o">.</span><span class="n">ResultsView</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">&#39;results&#39;</span><span class="p">),</span>
    <span class="n">url</span><span class="p">(</span><span class="s1">r&#39;^(?P&lt;question_id&gt;[0-9]+)/vote/$&#39;</span><span class="p">,</span> <span class="n">views</span><span class="o">.</span><span class="n">vote</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;vote&#39;</span><span class="p">),</span>
<span class="p">]</span>
</pre></div>
</div>
<p>Note that the name of the matched pattern in the regexes of the second and third
patterns has changed from <code class="docutils literal"><span class="pre">&lt;question_id&gt;</span></code> to <code class="docutils literal"><span class="pre">&lt;pk&gt;</span></code>.</p>
</div>
<div class="section" id="s-amend-views">
<span id="s-tutorial04-amend-views"></span><span id="amend-views"></span><span id="tutorial04-amend-views"></span><h3>Amend views<a class="headerlink" href="#amend-views" title="Permalink to this headline">¶</a></h3>
<p>Next, we&#8217;re going to remove our old <code class="docutils literal"><span class="pre">index</span></code>, <code class="docutils literal"><span class="pre">detail</span></code>, and <code class="docutils literal"><span class="pre">results</span></code>
views and use Django&#8217;s generic views instead. To do so, open the
<code class="docutils literal"><span class="pre">polls/views.py</span></code> file and change it like so:</p>
<div class="highlight-default"><div class="snippet-filename">polls/views.py</div>
<div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.shortcuts</span> <span class="k">import</span> <span class="n">get_object_or_404</span><span class="p">,</span> <span class="n">render</span>
<span class="kn">from</span> <span class="nn">django.http</span> <span class="k">import</span> <span class="n">HttpResponseRedirect</span>
<span class="kn">from</span> <span class="nn">django.core.urlresolvers</span> <span class="k">import</span> <span class="n">reverse</span>
<span class="kn">from</span> <span class="nn">django.views</span> <span class="k">import</span> <span class="n">generic</span>

<span class="kn">from</span> <span class="nn">.models</span> <span class="k">import</span> <span class="n">Choice</span><span class="p">,</span> <span class="n">Question</span>


<span class="k">class</span> <span class="nc">IndexView</span><span class="p">(</span><span class="n">generic</span><span class="o">.</span><span class="n">ListView</span><span class="p">):</span>
    <span class="n">template_name</span> <span class="o">=</span> <span class="s1">&#39;polls/index.html&#39;</span>
    <span class="n">context_object_name</span> <span class="o">=</span> <span class="s1">&#39;latest_question_list&#39;</span>

    <span class="k">def</span> <span class="nf">get_queryset</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return the last five published questions.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">Question</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">order_by</span><span class="p">(</span><span class="s1">&#39;-pub_date&#39;</span><span class="p">)[:</span><span class="mi">5</span><span class="p">]</span>


<span class="k">class</span> <span class="nc">DetailView</span><span class="p">(</span><span class="n">generic</span><span class="o">.</span><span class="n">DetailView</span><span class="p">):</span>
    <span class="n">model</span> <span class="o">=</span> <span class="n">Question</span>
    <span class="n">template_name</span> <span class="o">=</span> <span class="s1">&#39;polls/detail.html&#39;</span>


<span class="k">class</span> <span class="nc">ResultsView</span><span class="p">(</span><span class="n">generic</span><span class="o">.</span><span class="n">DetailView</span><span class="p">):</span>
    <span class="n">model</span> <span class="o">=</span> <span class="n">Question</span>
    <span class="n">template_name</span> <span class="o">=</span> <span class="s1">&#39;polls/results.html&#39;</span>


<span class="k">def</span> <span class="nf">vote</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">question_id</span><span class="p">):</span>
    <span class="o">...</span> <span class="c1"># same as above</span>
</pre></div>
</div>
<p>We&#8217;re using two generic views here:
<a class="reference internal" href="../ref/class-based-views/generic-display.html#django.views.generic.list.ListView" title="django.views.generic.list.ListView"><code class="xref py py-class docutils literal"><span class="pre">ListView</span></code></a> and
<a class="reference internal" href="../ref/class-based-views/generic-display.html#django.views.generic.detail.DetailView" title="django.views.generic.detail.DetailView"><code class="xref py py-class docutils literal"><span class="pre">DetailView</span></code></a>. Respectively, those
two views abstract the concepts of &#8220;display a list of objects&#8221; and
&#8220;display a detail page for a particular type of object.&#8221;</p>
<ul class="simple">
<li>Each generic view needs to know what model it will be acting
upon. This is provided using the <code class="docutils literal"><span class="pre">model</span></code> attribute.</li>
<li>The <a class="reference internal" href="../ref/class-based-views/generic-display.html#django.views.generic.detail.DetailView" title="django.views.generic.detail.DetailView"><code class="xref py py-class docutils literal"><span class="pre">DetailView</span></code></a> generic view
expects the primary key value captured from the URL to be called
<code class="docutils literal"><span class="pre">&quot;pk&quot;</span></code>, so we&#8217;ve changed <code class="docutils literal"><span class="pre">question_id</span></code> to <code class="docutils literal"><span class="pre">pk</span></code> for the generic
views.</li>
</ul>
<p>By default, the <a class="reference internal" href="../ref/class-based-views/generic-display.html#django.views.generic.detail.DetailView" title="django.views.generic.detail.DetailView"><code class="xref py py-class docutils literal"><span class="pre">DetailView</span></code></a> generic
view uses a template called <code class="docutils literal"><span class="pre">&lt;app</span> <span class="pre">name&gt;/&lt;model</span> <span class="pre">name&gt;_detail.html</span></code>.
In our case, it would use the template <code class="docutils literal"><span class="pre">&quot;polls/question_detail.html&quot;</span></code>. The
<code class="docutils literal"><span class="pre">template_name</span></code> attribute is used to tell Django to use a specific
template name instead of the autogenerated default template name. We
also specify the <code class="docutils literal"><span class="pre">template_name</span></code> for the <code class="docutils literal"><span class="pre">results</span></code> list view &#8211;
this ensures that the results view and the detail view have a
different appearance when rendered, even though they&#8217;re both a
<a class="reference internal" href="../ref/class-based-views/generic-display.html#django.views.generic.detail.DetailView" title="django.views.generic.detail.DetailView"><code class="xref py py-class docutils literal"><span class="pre">DetailView</span></code></a> behind the scenes.</p>
<p>Similarly, the <a class="reference internal" href="../ref/class-based-views/generic-display.html#django.views.generic.list.ListView" title="django.views.generic.list.ListView"><code class="xref py py-class docutils literal"><span class="pre">ListView</span></code></a> generic
view uses a default template called <code class="docutils literal"><span class="pre">&lt;app</span> <span class="pre">name&gt;/&lt;model</span>
<span class="pre">name&gt;_list.html</span></code>; we use <code class="docutils literal"><span class="pre">template_name</span></code> to tell
<a class="reference internal" href="../ref/class-based-views/generic-display.html#django.views.generic.list.ListView" title="django.views.generic.list.ListView"><code class="xref py py-class docutils literal"><span class="pre">ListView</span></code></a> to use our existing
<code class="docutils literal"><span class="pre">&quot;polls/index.html&quot;</span></code> template.</p>
<p>In previous parts of the tutorial, the templates have been provided
with a context that contains the <code class="docutils literal"><span class="pre">question</span></code> and <code class="docutils literal"><span class="pre">latest_question_list</span></code>
context variables. For <code class="docutils literal"><span class="pre">DetailView</span></code> the <code class="docutils literal"><span class="pre">question</span></code> variable is provided
automatically &#8211; since we&#8217;re using a Django model (<code class="docutils literal"><span class="pre">Question</span></code>), Django
is able to determine an appropriate name for the context variable.
However, for ListView, the automatically generated context variable is
<code class="docutils literal"><span class="pre">question_list</span></code>. To override this we provide the <code class="docutils literal"><span class="pre">context_object_name</span></code>
attribute, specifying that we want to use <code class="docutils literal"><span class="pre">latest_question_list</span></code> instead.
As an alternative approach, you could change your templates to match
the new default context variables &#8211; but it&#8217;s a lot easier to just
tell Django to use the variable you want.</p>
<p>Run the server, and use your new polling app based on generic views.</p>
<p>For full details on generic views, see the <a class="reference internal" href="../topics/class-based-views/index.html"><span class="doc">generic views documentation</span></a>.</p>
<p>When you&#8217;re comfortable with forms and generic views, read <a class="reference internal" href="tutorial05.html"><span class="doc">part 5 of this
tutorial</span></a> to learn about testing our polls app.</p>
</div>
</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="#">Writing your first Django app, part 4</a><ul>
<li><a class="reference internal" href="#write-a-simple-form">Write a simple form</a></li>
<li><a class="reference internal" href="#use-generic-views-less-code-is-better">Use generic views: Less code is better</a><ul>
<li><a class="reference internal" href="#amend-urlconf">Amend URLconf</a></li>
<li><a class="reference internal" href="#amend-views">Amend views</a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h3>Browse</h3>
  <ul>
    
      <li>Prev: <a href="tutorial03.html">Writing your first Django app, part 3</a></li>
    
    
      <li>Next: <a href="tutorial05.html">Writing your first Django app, part 5</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">Getting started</a>
        
        <ul><li>Writing your first Django app, part 4</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/intro/tutorial04.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">
    &laquo; <a href="tutorial03.html" title="Writing your first Django app, part 3">previous</a>
     |
    <a href="index.html" title="Getting started" accesskey="U">up</a>
   |
    <a href="tutorial05.html" title="Writing your first Django app, part 5">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>