
<!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="en">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>django.views.decorators.http &#8212; Django 2.0.14.dev20190701080343 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" id="documentation_options" data-url_root="../../../../" src="../../../../_static/documentation_options.js"></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/language_data.js"></script>
    <link rel="index" title="Index" href="../../../../genindex.html" />
    <link rel="search" title="Search" 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>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../../index.html">Django 2.0.14.dev20190701080343 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    <a href="../../../index.html" title="Module code" 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>Source code for 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">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">[docs]</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="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">[docs]</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">    The ETag function should return a complete ETag, including quotes (e.g.</span>
<span class="sd">    &#39;&quot;etag&quot;&#39;), since that&#39;s the only way to distinguish between weak and strong</span>
<span class="sd">    ETags. If an unquoted ETag is returned (e.g. &#39;etag&#39;), it will be converted</span>
<span class="sd">    to a strong ETag by adding quotes.</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 (precondition</span>
<span class="sd">    failed), depending upon the request method. In either case, the decorator</span>
<span class="sd">    will add the generated ETag and Last-Modified headers to the response if</span>
<span class="sd">    the headers aren&#39;t already set and if the request&#39;s method is safe.</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="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="c1"># The value from etag_func() could be quoted or unquoted.</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_etag</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">if</span> <span class="n">res_etag</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</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="c1"># and if the request method is safe.</span>
            <span class="k">if</span> <span class="n">request</span><span class="o">.</span><span class="n">method</span> <span class="ow">in</span> <span class="p">(</span><span class="s1">&#39;GET&#39;</span><span class="p">,</span> <span class="s1">&#39;HEAD&#39;</span><span class="p">):</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">res_etag</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">[docs]</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">[docs]</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">
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <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>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Jan 08, 2020</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    <a href="../../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>
  </div>

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