<!DOCTYPE html>

<html lang="en" data-content_root="../../../../">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>django.views.decorators.cache &#8212; Django 5.2 documentation</title>
    <link rel="stylesheet" type="text/css" href="../../../../_static/pygments.css?v=34bb78ad" />
    <link rel="stylesheet" type="text/css" href="../../../../_static/default.css?v=bf4d74af" />
    <script src="../../../../_static/documentation_options.js?v=6593ad68"></script>
    <script src="../../../../_static/doctools.js?v=9bcbadda"></script>
    <script src="../../../../_static/sphinx_highlight.js?v=dc90522c"></script>
    <link rel="index" title="Index" href="../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../search.html" />



 
<script src="../../../../templatebuiltins.js"></script>
<script>
(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 5.2 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-cache">
            
  <h1>Source code for django.views.decorators.cache</h1><div class="highlight"><pre>
<span></span><span class="kn">from</span><span class="w"> </span><span class="nn">functools</span><span class="w"> </span><span class="kn">import</span> <span class="n">wraps</span>

<span class="kn">from</span><span class="w"> </span><span class="nn">asgiref.sync</span><span class="w"> </span><span class="kn">import</span> <span class="n">iscoroutinefunction</span>

<span class="kn">from</span><span class="w"> </span><span class="nn">django.middleware.cache</span><span class="w"> </span><span class="kn">import</span> <span class="n">CacheMiddleware</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">django.utils.cache</span><span class="w"> </span><span class="kn">import</span> <span class="n">add_never_cache_headers</span><span class="p">,</span> <span class="n">patch_cache_control</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">django.utils.decorators</span><span class="w"> </span><span class="kn">import</span> <span class="n">decorator_from_middleware_with_args</span>


<span class="k">def</span><span class="w"> </span><span class="nf">cache_page</span><span class="p">(</span><span class="n">timeout</span><span class="p">,</span> <span class="o">*</span><span class="p">,</span> <span class="n">cache</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">key_prefix</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Decorator for views that tries getting the page from the cache and</span>
<span class="sd">    populates the cache if the page isn&#39;t in the cache yet.</span>

<span class="sd">    The cache is keyed by the URL and some data from the headers.</span>
<span class="sd">    Additionally there is the key prefix that is used to distinguish different</span>
<span class="sd">    cache areas in a multi-site setup. You could use the</span>
<span class="sd">    get_current_site().domain, for example, as that is unique across a Django</span>
<span class="sd">    project.</span>

<span class="sd">    Additionally, all headers from the response&#39;s Vary header will be taken</span>
<span class="sd">    into account on caching -- just like the middleware does.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">decorator_from_middleware_with_args</span><span class="p">(</span><span class="n">CacheMiddleware</span><span class="p">)(</span>
        <span class="n">page_timeout</span><span class="o">=</span><span class="n">timeout</span><span class="p">,</span>
        <span class="n">cache_alias</span><span class="o">=</span><span class="n">cache</span><span class="p">,</span>
        <span class="n">key_prefix</span><span class="o">=</span><span class="n">key_prefix</span><span class="p">,</span>
    <span class="p">)</span>


<span class="k">def</span><span class="w"> </span><span class="nf">_check_request</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">decorator_name</span><span class="p">):</span>
    <span class="c1"># Ensure argument looks like a request.</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="s2">&quot;META&quot;</span><span class="p">):</span>
        <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span>
            <span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">decorator_name</span><span class="si">}</span><span class="s2"> didn&#39;t receive an HttpRequest. If you are &quot;</span>
            <span class="s2">&quot;decorating a classmethod, be sure to use @method_decorator.&quot;</span>
        <span class="p">)</span>


<div class="viewcode-block" id="cache_control">
<a class="viewcode-back" href="../../../../topics/http/decorators.html#django.views.decorators.cache.cache_control">[docs]</a>
<span class="k">def</span><span class="w"> </span><span class="nf">cache_control</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
    <span class="k">def</span><span class="w"> </span><span class="nf">_cache_controller</span><span class="p">(</span><span class="n">viewfunc</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">iscoroutinefunction</span><span class="p">(</span><span class="n">viewfunc</span><span class="p">):</span>

            <span class="k">async</span> <span class="k">def</span><span class="w"> </span><span class="nf">_view_wrapper</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">kw</span><span class="p">):</span>
                <span class="n">_check_request</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="s2">&quot;cache_control&quot;</span><span class="p">)</span>
                <span class="n">response</span> <span class="o">=</span> <span class="k">await</span> <span class="n">viewfunc</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">kw</span><span class="p">)</span>
                <span class="n">patch_cache_control</span><span class="p">(</span><span class="n">response</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">response</span>

        <span class="k">else</span><span class="p">:</span>

            <span class="k">def</span><span class="w"> </span><span class="nf">_view_wrapper</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">kw</span><span class="p">):</span>
                <span class="n">_check_request</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="s2">&quot;cache_control&quot;</span><span class="p">)</span>
                <span class="n">response</span> <span class="o">=</span> <span class="n">viewfunc</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">kw</span><span class="p">)</span>
                <span class="n">patch_cache_control</span><span class="p">(</span><span class="n">response</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">response</span>

        <span class="k">return</span> <span class="n">wraps</span><span class="p">(</span><span class="n">viewfunc</span><span class="p">)(</span><span class="n">_view_wrapper</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">_cache_controller</span></div>



<div class="viewcode-block" id="never_cache">
<a class="viewcode-back" href="../../../../topics/http/decorators.html#django.views.decorators.cache.never_cache">[docs]</a>
<span class="k">def</span><span class="w"> </span><span class="nf">never_cache</span><span class="p">(</span><span class="n">view_func</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Decorator that adds headers to a response so that it will never be cached.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">if</span> <span class="n">iscoroutinefunction</span><span class="p">(</span><span class="n">view_func</span><span class="p">):</span>

        <span class="k">async</span> <span class="k">def</span><span class="w"> </span><span class="nf">_view_wrapper</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">_check_request</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="s2">&quot;never_cache&quot;</span><span class="p">)</span>
            <span class="n">response</span> <span class="o">=</span> <span class="k">await</span> <span class="n">view_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="n">add_never_cache_headers</span><span class="p">(</span><span class="n">response</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">response</span>

    <span class="k">else</span><span class="p">:</span>

        <span class="k">def</span><span class="w"> </span><span class="nf">_view_wrapper</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">_check_request</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="s2">&quot;never_cache&quot;</span><span class="p">)</span>
            <span class="n">response</span> <span class="o">=</span> <span class="n">view_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="n">add_never_cache_headers</span><span class="p">(</span><span class="n">response</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">response</span>

    <span class="k">return</span> <span class="n">wraps</span><span class="p">(</span><span class="n">view_func</span><span class="p">)(</span><span class="n">_view_wrapper</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">
        <div class="sphinxsidebarwrapper">
<search id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../../../search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
      <input type="submit" value="Go" />
    </form>
    </div>
</search>
<script>document.getElementById('searchbox').style.display = "block"</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Oct 01, 2025</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>