<!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>View decorators — Django v1.3.1 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.3.1', 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 v1.3.1 documentation" href="../../index.html" /> <link rel="up" title="Handling HTTP requests" href="index.html" /> <link rel="next" title="File Uploads" href="file-uploads.html" /> <link rel="prev" title="Writing views" href="views.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 v1.3.1 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="views.html" title="Writing views">previous</a> | <a href="../index.html" title="Using Django" accesskey="U">up</a> | <a href="file-uploads.html" title="File Uploads">next</a> »</div> </div> <div id="bd"> <div id="yui-main"> <div class="yui-b"> <div class="yui-g" id="topics-http-decorators"> <div class="section" id="s-module-django.views.decorators.http"> <span id="s-view-decorators"></span><span id="module-django.views.decorators.http"></span><span id="view-decorators"></span><h1>View decorators<a class="headerlink" href="#module-django.views.decorators.http" title="Permalink to this headline">¶</a></h1> <p>Django provides several decorators that can be applied to views to support various HTTP features.</p> <div class="section" id="s-allowed-http-methods"> <span id="allowed-http-methods"></span><h2>Allowed HTTP methods<a class="headerlink" href="#allowed-http-methods" title="Permalink to this headline">¶</a></h2> <p>The decorators in <a class="reference internal" href="#module-django.views.decorators.http" title="django.views.decorators.http"><tt class="xref py py-mod docutils literal"><span class="pre">django.views.decorators.http</span></tt></a> can be used to restrict access to views based on the request method. These decorators will return a <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">django.http.HttpResponseNotAllowed</span></tt></a> if the conditions are not met.</p> <dl class="function"> <dt id="django.views.decorators.http.require_http_methods"> <tt class="descname">require_http_methods</tt>(<em>request_method_list</em>)<a class="headerlink" href="#django.views.decorators.http.require_http_methods" title="Permalink to this definition">¶</a></dt> <dd></dd></dl> <p>This decorator is used to ensure that a view only accepts particular request methods. Usage:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.views.decorators.http</span> <span class="kn">import</span> <span class="n">require_http_methods</span> <span class="nd">@require_http_methods</span><span class="p">([</span><span class="s">"GET"</span><span class="p">,</span> <span class="s">"POST"</span><span class="p">])</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="c"># I can assume now that only GET or POST requests make it this far</span> <span class="c"># ...</span> <span class="k">pass</span> </pre></div> </div> <p>Note that request methods should be in uppercase.</p> <dl class="function"> <dt id="django.views.decorators.http.require_GET"> <tt class="descname">require_GET</tt>()<a class="headerlink" href="#django.views.decorators.http.require_GET" title="Permalink to this definition">¶</a></dt> <dd></dd></dl> <p>Decorator to require that a view only accept the GET method.</p> <dl class="function"> <dt id="django.views.decorators.http.require_POST"> <tt class="descname">require_POST</tt>()<a class="headerlink" href="#django.views.decorators.http.require_POST" title="Permalink to this definition">¶</a></dt> <dd></dd></dl> <p>Decorator to require that a view only accept the POST method.</p> </div> <div class="section" id="s-conditional-view-processing"> <span id="conditional-view-processing"></span><h2>Conditional view processing<a class="headerlink" href="#conditional-view-processing" title="Permalink to this headline">¶</a></h2> <p>The following decorators in <a class="reference internal" href="#module-django.views.decorators.http" title="django.views.decorators.http"><tt class="xref py py-mod docutils literal"><span class="pre">django.views.decorators.http</span></tt></a> can be used to control caching behavior on particular views.</p> <dl class="function"> <dt id="django.views.decorators.http.condition"> <tt class="descname">condition</tt>(<em>etag_func=None</em>, <em>last_modified_func=None</em>)<a class="headerlink" href="#django.views.decorators.http.condition" title="Permalink to this definition">¶</a></dt> <dd></dd></dl> <dl class="function"> <dt id="django.views.decorators.http.etag"> <tt class="descname">etag</tt>(<em>etag_func</em>)<a class="headerlink" href="#django.views.decorators.http.etag" title="Permalink to this definition">¶</a></dt> <dd></dd></dl> <dl class="function"> <dt id="django.views.decorators.http.last_modified"> <tt class="descname">last_modified</tt>(<em>last_modified_func</em>)<a class="headerlink" href="#django.views.decorators.http.last_modified" title="Permalink to this definition">¶</a></dt> <dd></dd></dl> <p>These decorators can be used to generate <tt class="docutils literal"><span class="pre">ETag</span></tt> and <tt class="docutils literal"><span class="pre">Last-Modified</span></tt> headers; see <a class="reference internal" href="../conditional-view-processing.html"><em>conditional view processing</em></a>.</p> <span class="target" id="module-django.views.decorators.gzip"></span></div> <div class="section" id="s-gzip-compression"> <span id="gzip-compression"></span><h2>GZip compression<a class="headerlink" href="#gzip-compression" title="Permalink to this headline">¶</a></h2> <p>The decorators in <a class="reference internal" href="#module-django.views.decorators.gzip" title="django.views.decorators.gzip"><tt class="xref py py-mod docutils literal"><span class="pre">django.views.decorators.gzip</span></tt></a> control content compression on a per-view basis.</p> <dl class="function"> <dt id="django.views.decorators.gzip.gzip_page"> <tt class="descname">gzip_page</tt>()<a class="headerlink" href="#django.views.decorators.gzip.gzip_page" title="Permalink to this definition">¶</a></dt> <dd></dd></dl> <p>This decorator compresses content if the browser allows gzip compression. It sets the <tt class="docutils literal"><span class="pre">Vary</span></tt> header accordingly, so that caches will base their storage on the <tt class="docutils literal"><span class="pre">Accept-Encoding</span></tt> header.</p> <span class="target" id="module-django.views.decorators.vary"></span></div> <div class="section" id="s-vary-headers"> <span id="vary-headers"></span><h2>Vary headers<a class="headerlink" href="#vary-headers" title="Permalink to this headline">¶</a></h2> <p>The decorators in <a class="reference internal" href="#module-django.views.decorators.vary" title="django.views.decorators.vary"><tt class="xref py py-mod docutils literal"><span class="pre">django.views.decorators.vary</span></tt></a> can be used to control caching based on specific request headers.</p> <dl class="function"> <dt id="django.views.decorators.vary.vary_on_cookie"> <tt class="descname">vary_on_cookie</tt>(<em>func</em>)<a class="headerlink" href="#django.views.decorators.vary.vary_on_cookie" title="Permalink to this definition">¶</a></dt> <dd></dd></dl> <dl class="function"> <dt id="django.views.decorators.vary.vary_on_headers"> <tt class="descname">vary_on_headers</tt>(<em>*headers</em>)<a class="headerlink" href="#django.views.decorators.vary.vary_on_headers" title="Permalink to this definition">¶</a></dt> <dd></dd></dl> <p>The <tt class="docutils literal"><span class="pre">Vary</span></tt> header defines which request headers a cache mechanism should take into account when building its cache key.</p> <p>See <a class="reference internal" href="../cache.html#using-vary-headers"><em>using vary headers</em></a>.</p> </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="#">View decorators</a><ul> <li><a class="reference internal" href="#allowed-http-methods">Allowed HTTP methods</a></li> <li><a class="reference internal" href="#conditional-view-processing">Conditional view processing</a></li> <li><a class="reference internal" href="#gzip-compression">GZip compression</a></li> <li><a class="reference internal" href="#vary-headers">Vary headers</a></li> </ul> </li> </ul> <h3>Browse</h3> <ul> <li>Prev: <a href="views.html">Writing views</a></li> <li>Next: <a href="file-uploads.html">File Uploads</a></li> </ul> <h3>You are here:</h3> <ul> <li> <a href="../../index.html">Django v1.3.1 documentation</a> <ul><li><a href="../index.html">Using Django</a> <ul><li><a href="index.html">Handling HTTP requests</a> <ul><li>View decorators</li></ul> </li></ul></li></ul> </li> </ul> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../../_sources/topics/http/decorators.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" size="18" /> <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 10, 2011</p> </div> </div> <div id="ft"> <div class="nav"> « <a href="views.html" title="Writing views">previous</a> | <a href="../index.html" title="Using Django" accesskey="U">up</a> | <a href="file-uploads.html" title="File Uploads">next</a> »</div> </div> </div> <div class="clearer"></div> </div> </body> </html>