Sophie

Sophie

distrib > Fedora > 20 > i386 > by-pkgid > 422242acff54b9373d7d4b7f73232ce1 > files > 767

python3-django-doc-1.6.7-1.fc20.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">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Introduction to Class-based views &mdash; Django 1.6.7 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.6.7',
        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.6.7 documentation" href="../../index.html" />
    <link rel="up" title="Class-based views" href="index.html" />
    <link rel="next" title="Built-in Class-based generic views" href="generic-display.html" />
    <link rel="prev" title="Class-based views" href="index.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>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../index.html">Django 1.6.7 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="index.html" title="Class-based views">previous</a> 
     |
    <a href="../index.html" title="Using Django" accesskey="U">up</a>
   |
    <a href="generic-display.html" title="Built-in Class-based generic views">next</a> &raquo;</div>
    </div>
    
    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="topics-class-based-views-intro">
            
  <div class="section" id="s-introduction-to-class-based-views">
<span id="introduction-to-class-based-views"></span><h1>Introduction to Class-based views<a class="headerlink" href="#introduction-to-class-based-views" title="Permalink to this headline">¶</a></h1>
<p>Class-based views provide an alternative way to implement views as Python
objects instead of functions. They do not replace function-based views, but
have certain differences and advantages when compared to function-based views:</p>
<ul class="simple">
<li>Organization of code related to specific HTTP methods (<tt class="docutils literal"><span class="pre">GET</span></tt>, <tt class="docutils literal"><span class="pre">POST</span></tt>,
etc) can be addressed by separate methods instead of conditional branching.</li>
<li>Object oriented techniques such as mixins (multiple inheritance) can be
used to factor code into reusable components.</li>
</ul>
<div class="section" id="s-the-relationship-and-history-of-generic-views-class-based-views-and-class-based-generic-views">
<span id="the-relationship-and-history-of-generic-views-class-based-views-and-class-based-generic-views"></span><h2>The relationship and history of generic views, class-based views, and class-based generic views<a class="headerlink" href="#the-relationship-and-history-of-generic-views-class-based-views-and-class-based-generic-views" title="Permalink to this headline">¶</a></h2>
<p>In the beginning there was only the view function contract, Django passed your
function an <a class="reference internal" href="../../ref/request-response.html#django.http.HttpRequest" title="django.http.HttpRequest"><tt class="xref py py-class docutils literal"><span class="pre">HttpRequest</span></tt></a> and expected back an
<a class="reference internal" href="../../ref/request-response.html#django.http.HttpResponse" title="django.http.HttpResponse"><tt class="xref py py-class docutils literal"><span class="pre">HttpResponse</span></tt></a>. This was the extent of what Django provided.</p>
<p>Early on it was recognized that there were common idioms and patterns found in
view development. Function-based generic views were introduced to abstract
these patterns and ease view development for the common cases.</p>
<p>The problem with function-based generic views is that while they covered the
simple cases well, there was no way to extend or customize them beyond some
simple configuration options, limiting their usefulness in many real-world
applications.</p>
<p>Class-based generic views were created with the same objective as
function-based generic views, to make view development easier. However, the way
the solution is implemented, through the use of mixins, provides a toolkit that
results in class-based generic views being more extensible and flexible than
their function-based counterparts.</p>
<p>If you have tried function based generic views in the past and found them
lacking, you should not think of class-based generic views as simply a
class-based equivalent, but rather as a fresh approach to solving the original
problems that generic views were meant to solve.</p>
<p>The toolkit of base classes and mixins that Django uses to build class-based
generic views are built for maximum flexibility, and as such have many hooks in
the form of default method implementations and attributes that you are unlikely
to be concerned with in the simplest use cases. For example, instead of
limiting you to a class based attribute for <tt class="docutils literal"><span class="pre">form_class</span></tt>, the implementation
uses a <tt class="docutils literal"><span class="pre">get_form</span></tt> method, which calls a <tt class="docutils literal"><span class="pre">get_form_class</span></tt> method, which in
its default implementation just returns the <tt class="docutils literal"><span class="pre">form_class</span></tt> attribute of the
class. This gives you several options for specifying what form to use, from a
simple attribute, to a fully dynamic, callable hook. These options seem to add
hollow complexity for simple situations, but without them, more advanced
designs would be limited.</p>
</div>
<div class="section" id="s-using-class-based-views">
<span id="using-class-based-views"></span><h2>Using class-based views<a class="headerlink" href="#using-class-based-views" title="Permalink to this headline">¶</a></h2>
<p>At its core, a class-based view allows you to respond to different HTTP request
methods with different class instance methods, instead of with conditionally
branching code inside a single view function.</p>
<p>So where the code to handle HTTP <tt class="docutils literal"><span class="pre">GET</span></tt> in a view function would look
something like:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.http</span> <span class="kn">import</span> <span class="n">HttpResponse</span>

<span class="k">def</span> <span class="nf">my_view</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">request</span><span class="o">.</span><span class="n">method</span> <span class="o">==</span> <span class="s">&#39;GET&#39;</span><span class="p">:</span>
        <span class="c"># &lt;view logic&gt;</span>
        <span class="k">return</span> <span class="n">HttpResponse</span><span class="p">(</span><span class="s">&#39;result&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>In a class-based view, this would become:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.http</span> <span class="kn">import</span> <span class="n">HttpResponse</span>
<span class="kn">from</span> <span class="nn">django.views.generic</span> <span class="kn">import</span> <span class="n">View</span>

<span class="k">class</span> <span class="nc">MyView</span><span class="p">(</span><span class="n">View</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">get</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">):</span>
        <span class="c"># &lt;view logic&gt;</span>
        <span class="k">return</span> <span class="n">HttpResponse</span><span class="p">(</span><span class="s">&#39;result&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>Because Django&#8217;s URL resolver expects to send the request and associated
arguments to a callable function, not a class, class-based views have an
<a class="reference internal" href="../../ref/class-based-views/base.html#django.views.generic.base.View.as_view" title="django.views.generic.base.View.as_view"><tt class="xref py py-meth docutils literal"><span class="pre">as_view()</span></tt></a> class method which serves as
the callable entry point to your class. The <tt class="docutils literal"><span class="pre">as_view</span></tt> entry point creates an
instance of your class and calls its
<a class="reference internal" href="../../ref/class-based-views/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> method. <tt class="docutils literal"><span class="pre">dispatch</span></tt> looks at
the request to determine whether it is a <tt class="docutils literal"><span class="pre">GET</span></tt>, <tt class="docutils literal"><span class="pre">POST</span></tt>, etc, and relays the
request to a matching method if one is defined, or raises
<a class="reference internal" href="../../ref/request-response.html#django.http.HttpResponseNotAllowed" title="django.http.HttpResponseNotAllowed"><tt class="xref py py-class docutils literal"><span class="pre">HttpResponseNotAllowed</span></tt></a> if not:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># urls.py</span>
<span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="kn">import</span> <span class="n">patterns</span>
<span class="kn">from</span> <span class="nn">myapp.views</span> <span class="kn">import</span> <span class="n">MyView</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">&#39;&#39;</span><span class="p">,</span>
    <span class="p">(</span><span class="s">r&#39;^about/&#39;</span><span class="p">,</span> <span class="n">MyView</span><span class="o">.</span><span class="n">as_view</span><span class="p">()),</span>
<span class="p">)</span>
</pre></div>
</div>
<p>It is worth noting that what your method returns is identical to what you
return from a function-based view, namely some form of
<a class="reference internal" href="../../ref/request-response.html#django.http.HttpResponse" title="django.http.HttpResponse"><tt class="xref py py-class docutils literal"><span class="pre">HttpResponse</span></tt></a>. This means that
<a class="reference internal" href="../http/shortcuts.html"><em>http shortcuts</em></a> or
<a class="reference internal" href="../../ref/template-response.html#django.template.response.TemplateResponse" title="django.template.response.TemplateResponse"><tt class="xref py py-class docutils literal"><span class="pre">TemplateResponse</span></tt></a> objects are valid to use
inside a class-based view.</p>
<p>While a minimal class-based view does not require any class attributes to
perform its job, class attributes are useful in many class-based designs,
and there are two ways to configure or set class attributes.</p>
<p>The first is the standard Python way of subclassing and overriding attributes
and methods in the subclass. So that if your parent class had an attribute
<tt class="docutils literal"><span class="pre">greeting</span></tt> like this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.http</span> <span class="kn">import</span> <span class="n">HttpResponse</span>
<span class="kn">from</span> <span class="nn">django.views.generic</span> <span class="kn">import</span> <span class="n">View</span>

<span class="k">class</span> <span class="nc">GreetingView</span><span class="p">(</span><span class="n">View</span><span class="p">):</span>
    <span class="n">greeting</span> <span class="o">=</span> <span class="s">&quot;Good Day&quot;</span>

    <span class="k">def</span> <span class="nf">get</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">HttpResponse</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">greeting</span><span class="p">)</span>
</pre></div>
</div>
<p>You can override that in a subclass:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">MorningGreetingView</span><span class="p">(</span><span class="n">GreetingView</span><span class="p">):</span>
    <span class="n">greeting</span> <span class="o">=</span> <span class="s">&quot;Morning to ya&quot;</span>
</pre></div>
</div>
<p>Another option is to configure class attributes as keyword arguments to the
<a class="reference internal" href="../../ref/class-based-views/base.html#django.views.generic.base.View.as_view" title="django.views.generic.base.View.as_view"><tt class="xref py py-meth docutils literal"><span class="pre">as_view()</span></tt></a> call in the URLconf:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">&#39;&#39;</span><span class="p">,</span>
    <span class="p">(</span><span class="s">r&#39;^about/&#39;</span><span class="p">,</span> <span class="n">GreetingView</span><span class="o">.</span><span class="n">as_view</span><span class="p">(</span><span class="n">greeting</span><span class="o">=</span><span class="s">&quot;G&#39;day&quot;</span><span class="p">)),</span>
<span class="p">)</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">While your class is instantiated for each request dispatched to it, class
attributes set through the
<a class="reference internal" href="../../ref/class-based-views/base.html#django.views.generic.base.View.as_view" title="django.views.generic.base.View.as_view"><tt class="xref py py-meth docutils literal"><span class="pre">as_view()</span></tt></a> entry point are
configured only once at the time your URLs are imported.</p>
</div>
</div>
<div class="section" id="s-using-mixins">
<span id="using-mixins"></span><h2>Using mixins<a class="headerlink" href="#using-mixins" title="Permalink to this headline">¶</a></h2>
<p>Mixins are a form of multiple inheritance where behaviors and attributes of
multiple parent classes can be combined.</p>
<p>For example, in the generic class-based views there is a mixin called
<a class="reference internal" href="../../ref/class-based-views/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> whose primary purpose
is to define the method
<a class="reference internal" href="../../ref/class-based-views/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>.
When combined with the behavior of the <a class="reference internal" href="../../ref/class-based-views/base.html#django.views.generic.base.View" title="django.views.generic.base.View"><tt class="xref py py-class docutils literal"><span class="pre">View</span></tt></a>
base class, the result is a <a class="reference internal" href="../../ref/class-based-views/base.html#django.views.generic.base.TemplateView" title="django.views.generic.base.TemplateView"><tt class="xref py py-class docutils literal"><span class="pre">TemplateView</span></tt></a>
class that will dispatch requests to the appropriate matching methods (a
behavior defined in the <tt class="docutils literal"><span class="pre">View</span></tt> base class), and that has a
<a class="reference internal" href="../../ref/class-based-views/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>
method that uses a
<a class="reference internal" href="../../ref/class-based-views/mixins-simple.html#django.views.generic.base.TemplateResponseMixin.template_name" title="django.views.generic.base.TemplateResponseMixin.template_name"><tt class="xref py py-attr docutils literal"><span class="pre">template_name</span></tt></a>
attribute to return a <a class="reference internal" href="../../ref/template-response.html#django.template.response.TemplateResponse" title="django.template.response.TemplateResponse"><tt class="xref py py-class docutils literal"><span class="pre">TemplateResponse</span></tt></a>
object (a behavior defined in the <tt class="docutils literal"><span class="pre">TemplateResponseMixin</span></tt>).</p>
<p>Mixins are an excellent way of reusing code across multiple classes, but they
come with some cost. The more your code is scattered among mixins, the harder
it will be to read a child class and know what exactly it is doing, and the
harder it will be to know which methods from which mixins to override if you
are subclassing something that has a deep inheritance tree.</p>
<p>Note also that you can only inherit from one generic view - that is, only one
parent class may inherit from <a class="reference internal" href="../../ref/class-based-views/base.html#django.views.generic.base.View" title="django.views.generic.base.View"><tt class="xref py py-class docutils literal"><span class="pre">View</span></tt></a> and
the rest (if any) should be mixins. Trying to inherit from more than one class
that inherits from <tt class="docutils literal"><span class="pre">View</span></tt> - for example, trying to use a form at the top of a
list and combining <a class="reference internal" href="../../ref/class-based-views/mixins-editing.html#django.views.generic.edit.ProcessFormView" title="django.views.generic.edit.ProcessFormView"><tt class="xref py py-class docutils literal"><span class="pre">ProcessFormView</span></tt></a> and
<a class="reference internal" href="../../ref/class-based-views/generic-display.html#django.views.generic.list.ListView" title="django.views.generic.list.ListView"><tt class="xref py py-class docutils literal"><span class="pre">ListView</span></tt></a> - won&#8217;t work as expected.</p>
</div>
<div class="section" id="s-handling-forms-with-class-based-views">
<span id="handling-forms-with-class-based-views"></span><h2>Handling forms with class-based views<a class="headerlink" href="#handling-forms-with-class-based-views" title="Permalink to this headline">¶</a></h2>
<p>A basic function-based view that handles forms may look something like this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.http</span> <span class="kn">import</span> <span class="n">HttpResponseRedirect</span>
<span class="kn">from</span> <span class="nn">django.shortcuts</span> <span class="kn">import</span> <span class="n">render</span>

<span class="kn">from</span> <span class="nn">.forms</span> <span class="kn">import</span> <span class="n">MyForm</span>

<span class="k">def</span> <span class="nf">myview</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">request</span><span class="o">.</span><span class="n">method</span> <span class="o">==</span> <span class="s">&quot;POST&quot;</span><span class="p">:</span>
        <span class="n">form</span> <span class="o">=</span> <span class="n">MyForm</span><span class="p">(</span><span class="n">request</span><span class="o">.</span><span class="n">POST</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">form</span><span class="o">.</span><span class="n">is_valid</span><span class="p">():</span>
            <span class="c"># &lt;process form cleaned data&gt;</span>
            <span class="k">return</span> <span class="n">HttpResponseRedirect</span><span class="p">(</span><span class="s">&#39;/success/&#39;</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">form</span> <span class="o">=</span> <span class="n">MyForm</span><span class="p">(</span><span class="n">initial</span><span class="o">=</span><span class="p">{</span><span class="s">&#39;key&#39;</span><span class="p">:</span> <span class="s">&#39;value&#39;</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="s">&#39;form_template.html&#39;</span><span class="p">,</span> <span class="p">{</span><span class="s">&#39;form&#39;</span><span class="p">:</span> <span class="n">form</span><span class="p">})</span>
</pre></div>
</div>
<p>A similar class-based view might look like:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.http</span> <span class="kn">import</span> <span class="n">HttpResponseRedirect</span>
<span class="kn">from</span> <span class="nn">django.shortcuts</span> <span class="kn">import</span> <span class="n">render</span>
<span class="kn">from</span> <span class="nn">django.views.generic</span> <span class="kn">import</span> <span class="n">View</span>

<span class="kn">from</span> <span class="nn">.forms</span> <span class="kn">import</span> <span class="n">MyForm</span>

<span class="k">class</span> <span class="nc">MyFormView</span><span class="p">(</span><span class="n">View</span><span class="p">):</span>
    <span class="n">form_class</span> <span class="o">=</span> <span class="n">MyForm</span>
    <span class="n">initial</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;key&#39;</span><span class="p">:</span> <span class="s">&#39;value&#39;</span><span class="p">}</span>
    <span class="n">template_name</span> <span class="o">=</span> <span class="s">&#39;form_template.html&#39;</span>

    <span class="k">def</span> <span class="nf">get</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="n">form</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">form_class</span><span class="p">(</span><span class="n">initial</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">initial</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="bp">self</span><span class="o">.</span><span class="n">template_name</span><span class="p">,</span> <span class="p">{</span><span class="s">&#39;form&#39;</span><span class="p">:</span> <span class="n">form</span><span class="p">})</span>

    <span class="k">def</span> <span class="nf">post</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="n">form</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">form_class</span><span class="p">(</span><span class="n">request</span><span class="o">.</span><span class="n">POST</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">form</span><span class="o">.</span><span class="n">is_valid</span><span class="p">():</span>
            <span class="c"># &lt;process form cleaned data&gt;</span>
            <span class="k">return</span> <span class="n">HttpResponseRedirect</span><span class="p">(</span><span class="s">&#39;/success/&#39;</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="bp">self</span><span class="o">.</span><span class="n">template_name</span><span class="p">,</span> <span class="p">{</span><span class="s">&#39;form&#39;</span><span class="p">:</span> <span class="n">form</span><span class="p">})</span>
</pre></div>
</div>
<p>This is a very simple case, but you can see that you would then have the option
of customizing this view by overriding any of the class attributes, e.g.
<tt class="docutils literal"><span class="pre">form_class</span></tt>, via URLconf configuration, or subclassing and overriding one or
more of the methods (or both!).</p>
</div>
<div class="section" id="s-decorating-class-based-views">
<span id="decorating-class-based-views"></span><h2>Decorating class-based views<a class="headerlink" href="#decorating-class-based-views" title="Permalink to this headline">¶</a></h2>
<p>The extension of class-based views isn&#8217;t limited to using mixins. You
can use also use decorators. Since class-based views aren&#8217;t functions,
decorating them works differently depending on if you&#8217;re using <tt class="docutils literal"><span class="pre">as_view</span></tt> or
creating a subclass.</p>
<div class="section" id="s-decorating-in-urlconf">
<span id="decorating-in-urlconf"></span><h3>Decorating in URLconf<a class="headerlink" href="#decorating-in-urlconf" title="Permalink to this headline">¶</a></h3>
<p>The simplest way of decorating class-based views is to decorate the
result of the <a class="reference internal" href="../../ref/class-based-views/base.html#django.views.generic.base.View.as_view" title="django.views.generic.base.View.as_view"><tt class="xref py py-meth docutils literal"><span class="pre">as_view()</span></tt></a> method.
The easiest place to do this is in the URLconf where you deploy your view:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.contrib.auth.decorators</span> <span class="kn">import</span> <span class="n">login_required</span><span class="p">,</span> <span class="n">permission_required</span>
<span class="kn">from</span> <span class="nn">django.views.generic</span> <span class="kn">import</span> <span class="n">TemplateView</span>

<span class="kn">from</span> <span class="nn">.views</span> <span class="kn">import</span> <span class="n">VoteView</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">&#39;&#39;</span><span class="p">,</span>
    <span class="p">(</span><span class="s">r&#39;^about/&#39;</span><span class="p">,</span> <span class="n">login_required</span><span class="p">(</span><span class="n">TemplateView</span><span class="o">.</span><span class="n">as_view</span><span class="p">(</span><span class="n">template_name</span><span class="o">=</span><span class="s">&quot;secret.html&quot;</span><span class="p">))),</span>
    <span class="p">(</span><span class="s">r&#39;^vote/&#39;</span><span class="p">,</span> <span class="n">permission_required</span><span class="p">(</span><span class="s">&#39;polls.can_vote&#39;</span><span class="p">)(</span><span class="n">VoteView</span><span class="o">.</span><span class="n">as_view</span><span class="p">())),</span>
<span class="p">)</span>
</pre></div>
</div>
<p>This approach applies the decorator on a per-instance basis. If you
want every instance of a view to be decorated, you need to take a
different approach.</p>
</div>
<div class="section" id="s-decorating-the-class">
<span id="s-id1"></span><span id="decorating-the-class"></span><span id="id1"></span><h3>Decorating the class<a class="headerlink" href="#decorating-the-class" title="Permalink to this headline">¶</a></h3>
<p>To decorate every instance of a class-based view, you need to decorate
the class definition itself. To do this you apply the decorator to the
<a class="reference internal" href="../../ref/class-based-views/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> method of the class.</p>
<p>A method on a class isn&#8217;t quite the same as a standalone function, so
you can&#8217;t just apply a function decorator to the method &#8211; you need to
transform it into a method decorator first. The <tt class="docutils literal"><span class="pre">method_decorator</span></tt>
decorator transforms a function decorator into a method decorator so
that it can be used on an instance method. For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.contrib.auth.decorators</span> <span class="kn">import</span> <span class="n">login_required</span>
<span class="kn">from</span> <span class="nn">django.utils.decorators</span> <span class="kn">import</span> <span class="n">method_decorator</span>
<span class="kn">from</span> <span class="nn">django.views.generic</span> <span class="kn">import</span> <span class="n">TemplateView</span>

<span class="k">class</span> <span class="nc">ProtectedView</span><span class="p">(</span><span class="n">TemplateView</span><span class="p">):</span>
    <span class="n">template_name</span> <span class="o">=</span> <span class="s">&#39;secret.html&#39;</span>

    <span class="nd">@method_decorator</span><span class="p">(</span><span class="n">login_required</span><span class="p">)</span>
    <span class="k">def</span> <span class="nf">dispatch</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">(</span><span class="n">ProtectedView</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">dispatch</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
</pre></div>
</div>
<p>In this example, every instance of <tt class="docutils literal"><span class="pre">ProtectedView</span></tt> will have
login protection.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last"><tt class="docutils literal"><span class="pre">method_decorator</span></tt> passes <tt class="docutils literal"><span class="pre">*args</span></tt> and <tt class="docutils literal"><span class="pre">**kwargs</span></tt>
as parameters to the decorated method on the class. If your method
does not accept a compatible set of parameters it will raise a
<tt class="docutils literal"><span class="pre">TypeError</span></tt> exception.</p>
</div>
</div>
</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="#">Introduction to Class-based views</a><ul>
<li><a class="reference internal" href="#the-relationship-and-history-of-generic-views-class-based-views-and-class-based-generic-views">The relationship and history of generic views, class-based views, and class-based generic views</a></li>
<li><a class="reference internal" href="#using-class-based-views">Using class-based views</a></li>
<li><a class="reference internal" href="#using-mixins">Using mixins</a></li>
<li><a class="reference internal" href="#handling-forms-with-class-based-views">Handling forms with class-based views</a></li>
<li><a class="reference internal" href="#decorating-class-based-views">Decorating class-based views</a><ul>
<li><a class="reference internal" href="#decorating-in-urlconf">Decorating in URLconf</a></li>
<li><a class="reference internal" href="#decorating-the-class">Decorating the class</a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h3>Browse</h3>
  <ul>
    
      <li>Prev: <a href="index.html">Class-based views</a></li>
    
    
      <li>Next: <a href="generic-display.html">Built-in Class-based generic views</a></li>
    
  </ul>
  <h3>You are here:</h3>
  <ul>
      <li>
        <a href="../../index.html">Django 1.6.7 documentation</a>
        
          <ul><li><a href="../index.html">Using Django</a>
        
          <ul><li><a href="index.html">Class-based views</a>
        
        <ul><li>Introduction to Class-based views</li></ul>
        </li></ul></li></ul>
      </li>
  </ul>  

  <h3>This Page</h3>
  <ul class="this-page-menu">
    <li><a href="../../_sources/topics/class-based-views/intro.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">Sep 26, 2014</p>
          </div> 
        
      
    </div>
    
    <div id="ft">
      <div class="nav">
    &laquo; <a href="index.html" title="Class-based views">previous</a> 
     |
    <a href="../index.html" title="Using Django" accesskey="U">up</a>
   |
    <a href="generic-display.html" title="Built-in Class-based generic views">next</a> &raquo;</div>
    </div>
  </div>

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