<!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="es">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>django.views.decorators.http &#8212; documentación de Django - 1.10.7</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.10.7',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true,
        SOURCELINK_SUFFIX: '.txt'
      };
    </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>
    <script type="text/javascript" src="../../../../_static/translations.js"></script>
    <link rel="index" title="Índice" href="../../../../genindex.html" />
    <link rel="search" title="Búsqueda" href="../../../../search.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">documentación de Django - 1.10.7</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="../../../index.html" title="Código de módulo" accesskey="U">up</a></div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="_modules-django-views-decorators-http">
            
  <h1>Código fuente para django.views.decorators.http</h1><div class="highlight"><pre>
<span></span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">Decorators for views based on HTTP headers.</span>
<span class="sd">&quot;&quot;&quot;</span>

<span class="kn">import</span> <span class="nn">logging</span>
<span class="kn">from</span> <span class="nn">calendar</span> <span class="k">import</span> <span class="n">timegm</span>
<span class="kn">from</span> <span class="nn">functools</span> <span class="k">import</span> <span class="n">wraps</span>

<span class="kn">from</span> <span class="nn">django.http</span> <span class="k">import</span> <span class="n">HttpResponseNotAllowed</span>
<span class="kn">from</span> <span class="nn">django.middleware.http</span> <span class="k">import</span> <span class="n">ConditionalGetMiddleware</span>
<span class="kn">from</span> <span class="nn">django.utils.cache</span> <span class="k">import</span> <span class="n">get_conditional_response</span>
<span class="kn">from</span> <span class="nn">django.utils.decorators</span> <span class="k">import</span> <span class="n">available_attrs</span><span class="p">,</span> <span class="n">decorator_from_middleware</span>
<span class="kn">from</span> <span class="nn">django.utils.http</span> <span class="k">import</span> <span class="n">http_date</span><span class="p">,</span> <span class="n">quote_etag</span>

<span class="n">conditional_page</span> <span class="o">=</span> <span class="n">decorator_from_middleware</span><span class="p">(</span><span class="n">ConditionalGetMiddleware</span><span class="p">)</span>

<span class="n">logger</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="s1">&#39;django.request&#39;</span><span class="p">)</span>


<div class="viewcode-block" id="require_http_methods"><a class="viewcode-back" href="../../../../topics/http/decorators.html#django.views.decorators.http.require_http_methods">[documentos]</a><span class="k">def</span> <span class="nf">require_http_methods</span><span class="p">(</span><span class="n">request_method_list</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Decorator to make a view only accept particular request methods.  Usage::</span>

<span class="sd">        @require_http_methods([&quot;GET&quot;, &quot;POST&quot;])</span>
<span class="sd">        def my_view(request):</span>
<span class="sd">            # I can assume now that only GET or POST requests make it this far</span>
<span class="sd">            # ...</span>

<span class="sd">    Note that request methods should be in uppercase.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">decorator</span><span class="p">(</span><span class="n">func</span><span class="p">):</span>
        <span class="nd">@wraps</span><span class="p">(</span><span class="n">func</span><span class="p">,</span> <span class="n">assigned</span><span class="o">=</span><span class="n">available_attrs</span><span class="p">(</span><span class="n">func</span><span class="p">))</span>
        <span class="k">def</span> <span class="nf">inner</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="k">if</span> <span class="n">request</span><span class="o">.</span><span class="n">method</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">request_method_list</span><span class="p">:</span>
                <span class="n">logger</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span>
                    <span class="s1">&#39;Method Not Allowed (</span><span class="si">%s</span><span class="s1">): </span><span class="si">%s</span><span class="s1">&#39;</span><span class="p">,</span> <span class="n">request</span><span class="o">.</span><span class="n">method</span><span class="p">,</span> <span class="n">request</span><span class="o">.</span><span class="n">path</span><span class="p">,</span>
                    <span class="n">extra</span><span class="o">=</span><span class="p">{</span><span class="s1">&#39;status_code&#39;</span><span class="p">:</span> <span class="mi">405</span><span class="p">,</span> <span class="s1">&#39;request&#39;</span><span class="p">:</span> <span class="n">request</span><span class="p">}</span>
                <span class="p">)</span>
                <span class="k">return</span> <span class="n">HttpResponseNotAllowed</span><span class="p">(</span><span class="n">request_method_list</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">func</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="k">return</span> <span class="n">inner</span>
    <span class="k">return</span> <span class="n">decorator</span></div>

<span class="n">require_GET</span> <span class="o">=</span> <span class="n">require_http_methods</span><span class="p">([</span><span class="s2">&quot;GET&quot;</span><span class="p">])</span>
<span class="n">require_GET</span><span class="o">.</span><span class="vm">__doc__</span> <span class="o">=</span> <span class="s2">&quot;Decorator to require that a view only accepts the GET method.&quot;</span>

<span class="n">require_POST</span> <span class="o">=</span> <span class="n">require_http_methods</span><span class="p">([</span><span class="s2">&quot;POST&quot;</span><span class="p">])</span>
<span class="n">require_POST</span><span class="o">.</span><span class="vm">__doc__</span> <span class="o">=</span> <span class="s2">&quot;Decorator to require that a view only accepts the POST method.&quot;</span>

<span class="n">require_safe</span> <span class="o">=</span> <span class="n">require_http_methods</span><span class="p">([</span><span class="s2">&quot;GET&quot;</span><span class="p">,</span> <span class="s2">&quot;HEAD&quot;</span><span class="p">])</span>
<span class="n">require_safe</span><span class="o">.</span><span class="vm">__doc__</span> <span class="o">=</span> <span class="s2">&quot;Decorator to require that a view only accepts safe methods: GET and HEAD.&quot;</span>


<div class="viewcode-block" id="condition"><a class="viewcode-back" href="../../../../topics/http/decorators.html#django.views.decorators.http.condition">[documentos]</a><span class="k">def</span> <span class="nf">condition</span><span class="p">(</span><span class="n">etag_func</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">last_modified_func</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Decorator to support conditional retrieval (or change) for a view</span>
<span class="sd">    function.</span>

<span class="sd">    The parameters are callables to compute the ETag and last modified time for</span>
<span class="sd">    the requested resource, respectively. The callables are passed the same</span>
<span class="sd">    parameters as the view itself. The ETag function should return a string (or</span>
<span class="sd">    None if the resource doesn&#39;t exist), while the last_modified function</span>
<span class="sd">    should return a datetime object (or None if the resource doesn&#39;t exist).</span>

<span class="sd">    If both parameters are provided, all the preconditions must be met before</span>
<span class="sd">    the view is processed.</span>

<span class="sd">    This decorator will either pass control to the wrapped view function or</span>
<span class="sd">    return an HTTP 304 response (unmodified) or 412 response (preconditions</span>
<span class="sd">    failed), depending upon the request method.</span>

<span class="sd">    Any behavior marked as &quot;undefined&quot; in the HTTP spec (e.g. If-none-match</span>
<span class="sd">    plus If-modified-since headers) will result in the view function being</span>
<span class="sd">    called.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">decorator</span><span class="p">(</span><span class="n">func</span><span class="p">):</span>
        <span class="nd">@wraps</span><span class="p">(</span><span class="n">func</span><span class="p">,</span> <span class="n">assigned</span><span class="o">=</span><span class="n">available_attrs</span><span class="p">(</span><span class="n">func</span><span class="p">))</span>
        <span class="k">def</span> <span class="nf">inner</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="c1"># Compute values (if any) for the requested resource.</span>
            <span class="k">def</span> <span class="nf">get_last_modified</span><span class="p">():</span>
                <span class="k">if</span> <span class="n">last_modified_func</span><span class="p">:</span>
                    <span class="n">dt</span> <span class="o">=</span> <span class="n">last_modified_func</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="k">if</span> <span class="n">dt</span><span class="p">:</span>
                        <span class="k">return</span> <span class="n">timegm</span><span class="p">(</span><span class="n">dt</span><span class="o">.</span><span class="n">utctimetuple</span><span class="p">())</span>

            <span class="n">res_etag</span> <span class="o">=</span> <span class="n">etag_func</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="k">if</span> <span class="n">etag_func</span> <span class="k">else</span> <span class="kc">None</span>
            <span class="n">res_last_modified</span> <span class="o">=</span> <span class="n">get_last_modified</span><span class="p">()</span>

            <span class="n">response</span> <span class="o">=</span> <span class="n">get_conditional_response</span><span class="p">(</span>
                <span class="n">request</span><span class="p">,</span>
                <span class="n">etag</span><span class="o">=</span><span class="n">res_etag</span><span class="p">,</span>
                <span class="n">last_modified</span><span class="o">=</span><span class="n">res_last_modified</span><span class="p">,</span>
            <span class="p">)</span>

            <span class="k">if</span> <span class="n">response</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">response</span> <span class="o">=</span> <span class="n">func</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="c1"># Set relevant headers on the response if they don&#39;t already exist.</span>
            <span class="k">if</span> <span class="n">res_last_modified</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">response</span><span class="o">.</span><span class="n">has_header</span><span class="p">(</span><span class="s1">&#39;Last-Modified&#39;</span><span class="p">):</span>
                <span class="n">response</span><span class="p">[</span><span class="s1">&#39;Last-Modified&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">http_date</span><span class="p">(</span><span class="n">res_last_modified</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">res_etag</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">response</span><span class="o">.</span><span class="n">has_header</span><span class="p">(</span><span class="s1">&#39;ETag&#39;</span><span class="p">):</span>
                <span class="n">response</span><span class="p">[</span><span class="s1">&#39;ETag&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">quote_etag</span><span class="p">(</span><span class="n">res_etag</span><span class="p">)</span>

            <span class="k">return</span> <span class="n">response</span>

        <span class="k">return</span> <span class="n">inner</span>
    <span class="k">return</span> <span class="n">decorator</span></div>


<span class="c1"># Shortcut decorators for common cases based on ETag or Last-Modified only</span>
<div class="viewcode-block" id="etag"><a class="viewcode-back" href="../../../../topics/http/decorators.html#django.views.decorators.http.etag">[documentos]</a><span class="k">def</span> <span class="nf">etag</span><span class="p">(</span><span class="n">etag_func</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">condition</span><span class="p">(</span><span class="n">etag_func</span><span class="o">=</span><span class="n">etag_func</span><span class="p">)</span></div>


<div class="viewcode-block" id="last_modified"><a class="viewcode-back" href="../../../../topics/http/decorators.html#django.views.decorators.http.last_modified">[documentos]</a><span class="k">def</span> <span class="nf">last_modified</span><span class="p">(</span><span class="n">last_modified_func</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">condition</span><span class="p">(</span><span class="n">last_modified_func</span><span class="o">=</span><span class="n">last_modified_func</span><span class="p">)</span></div>
</pre></div>

          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3>Browse</h3>
  <ul>
    
    
  </ul>
  <h3>You are here:</h3>
  <ul>
      <li>
        <a href="../../../../index.html">documentación de Django - 1.10.7</a>
        
          <ul><li><a href="../../../index.html">Código de módulo</a>
        
          <ul><li><a href="../../../django.html">django</a>
        
        <ul><li>django.views.decorators.http</li></ul>
        </li></ul></li></ul>
      </li>
  </ul>

<div id="searchbox" style="display: none" role="search">
  <h3>Búsqueda rápida</h3>
    <form class="search" action="../../../../search.html" method="get">
      <div><input type="text" name="q" /></div>
      <div><input type="submit" value="Ir a" /></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">abr. 04, 2017</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    <a href="../../../index.html" title="Código de módulo" accesskey="U">up</a></div>
    </div>
  </div>

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