<!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.defaults &#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-defaults">
            
  <h1>Source code for django.views.defaults</h1><div class="highlight"><pre>
<span></span><span class="kn">from</span><span class="w"> </span><span class="nn">urllib.parse</span><span class="w"> </span><span class="kn">import</span> <span class="n">quote</span>

<span class="kn">from</span><span class="w"> </span><span class="nn">django.http</span><span class="w"> </span><span class="kn">import</span> <span class="p">(</span>
    <span class="n">HttpResponseBadRequest</span><span class="p">,</span>
    <span class="n">HttpResponseForbidden</span><span class="p">,</span>
    <span class="n">HttpResponseNotFound</span><span class="p">,</span>
    <span class="n">HttpResponseServerError</span><span class="p">,</span>
<span class="p">)</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">django.template</span><span class="w"> </span><span class="kn">import</span> <span class="n">Context</span><span class="p">,</span> <span class="n">Engine</span><span class="p">,</span> <span class="n">TemplateDoesNotExist</span><span class="p">,</span> <span class="n">loader</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">django.views.decorators.csrf</span><span class="w"> </span><span class="kn">import</span> <span class="n">requires_csrf_token</span>

<span class="n">ERROR_404_TEMPLATE_NAME</span> <span class="o">=</span> <span class="s2">&quot;404.html&quot;</span>
<span class="n">ERROR_403_TEMPLATE_NAME</span> <span class="o">=</span> <span class="s2">&quot;403.html&quot;</span>
<span class="n">ERROR_400_TEMPLATE_NAME</span> <span class="o">=</span> <span class="s2">&quot;400.html&quot;</span>
<span class="n">ERROR_500_TEMPLATE_NAME</span> <span class="o">=</span> <span class="s2">&quot;500.html&quot;</span>
<span class="n">ERROR_PAGE_TEMPLATE</span> <span class="o">=</span> <span class="s2">&quot;&quot;&quot;</span>
<span class="s2">&lt;!doctype html&gt;</span>
<span class="s2">&lt;html lang=&quot;en&quot;&gt;</span>
<span class="s2">&lt;head&gt;</span>
<span class="s2">  &lt;title&gt;</span><span class="si">%(title)s</span><span class="s2">&lt;/title&gt;</span>
<span class="s2">&lt;/head&gt;</span>
<span class="s2">&lt;body&gt;</span>
<span class="s2">  &lt;h1&gt;</span><span class="si">%(title)s</span><span class="s2">&lt;/h1&gt;&lt;p&gt;</span><span class="si">%(details)s</span><span class="s2">&lt;/p&gt;</span>
<span class="s2">&lt;/body&gt;</span>
<span class="s2">&lt;/html&gt;</span>
<span class="s2">&quot;&quot;&quot;</span>


<span class="c1"># These views can be called when CsrfViewMiddleware.process_view() not run,</span>
<span class="c1"># therefore need @requires_csrf_token in case the template needs</span>
<span class="c1"># {% csrf_token %}.</span>


<span class="nd">@requires_csrf_token</span>
<span class="k">def</span><span class="w"> </span><span class="nf">page_not_found</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">exception</span><span class="p">,</span> <span class="n">template_name</span><span class="o">=</span><span class="n">ERROR_404_TEMPLATE_NAME</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Default 404 handler.</span>

<span class="sd">    Templates: :template:`404.html`</span>
<span class="sd">    Context:</span>
<span class="sd">        request_path</span>
<span class="sd">            The path of the requested URL (e.g., &#39;/app/pages/bad_page/&#39;). It&#39;s</span>
<span class="sd">            quoted to prevent a content injection attack.</span>
<span class="sd">        exception</span>
<span class="sd">            The message from the exception which triggered the 404 (if one was</span>
<span class="sd">            supplied), or the exception class name</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">exception_repr</span> <span class="o">=</span> <span class="n">exception</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span>
    <span class="c1"># Try to get an &quot;interesting&quot; exception message, if any (and not the ugly</span>
    <span class="c1"># Resolver404 dictionary)</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">message</span> <span class="o">=</span> <span class="n">exception</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
    <span class="k">except</span> <span class="p">(</span><span class="ne">AttributeError</span><span class="p">,</span> <span class="ne">IndexError</span><span class="p">):</span>
        <span class="k">pass</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">message</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>
            <span class="n">exception_repr</span> <span class="o">=</span> <span class="n">message</span>
    <span class="n">context</span> <span class="o">=</span> <span class="p">{</span>
        <span class="s2">&quot;request_path&quot;</span><span class="p">:</span> <span class="n">quote</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="s2">&quot;exception&quot;</span><span class="p">:</span> <span class="n">exception_repr</span><span class="p">,</span>
    <span class="p">}</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">template</span> <span class="o">=</span> <span class="n">loader</span><span class="o">.</span><span class="n">get_template</span><span class="p">(</span><span class="n">template_name</span><span class="p">)</span>
        <span class="n">body</span> <span class="o">=</span> <span class="n">template</span><span class="o">.</span><span class="n">render</span><span class="p">(</span><span class="n">context</span><span class="p">,</span> <span class="n">request</span><span class="p">)</span>
    <span class="k">except</span> <span class="n">TemplateDoesNotExist</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">template_name</span> <span class="o">!=</span> <span class="n">ERROR_404_TEMPLATE_NAME</span><span class="p">:</span>
            <span class="c1"># Reraise if it&#39;s a missing custom template.</span>
            <span class="k">raise</span>
        <span class="c1"># Render template (even though there are no substitutions) to allow</span>
        <span class="c1"># inspecting the context in tests.</span>
        <span class="n">template</span> <span class="o">=</span> <span class="n">Engine</span><span class="p">()</span><span class="o">.</span><span class="n">from_string</span><span class="p">(</span>
            <span class="n">ERROR_PAGE_TEMPLATE</span>
            <span class="o">%</span> <span class="p">{</span>
                <span class="s2">&quot;title&quot;</span><span class="p">:</span> <span class="s2">&quot;Not Found&quot;</span><span class="p">,</span>
                <span class="s2">&quot;details&quot;</span><span class="p">:</span> <span class="s2">&quot;The requested resource was not found on this server.&quot;</span><span class="p">,</span>
            <span class="p">},</span>
        <span class="p">)</span>
        <span class="n">body</span> <span class="o">=</span> <span class="n">template</span><span class="o">.</span><span class="n">render</span><span class="p">(</span><span class="n">Context</span><span class="p">(</span><span class="n">context</span><span class="p">))</span>
    <span class="k">return</span> <span class="n">HttpResponseNotFound</span><span class="p">(</span><span class="n">body</span><span class="p">)</span>


<span class="nd">@requires_csrf_token</span>
<span class="k">def</span><span class="w"> </span><span class="nf">server_error</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">template_name</span><span class="o">=</span><span class="n">ERROR_500_TEMPLATE_NAME</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    500 error handler.</span>

<span class="sd">    Templates: :template:`500.html`</span>
<span class="sd">    Context: None</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">template</span> <span class="o">=</span> <span class="n">loader</span><span class="o">.</span><span class="n">get_template</span><span class="p">(</span><span class="n">template_name</span><span class="p">)</span>
    <span class="k">except</span> <span class="n">TemplateDoesNotExist</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">template_name</span> <span class="o">!=</span> <span class="n">ERROR_500_TEMPLATE_NAME</span><span class="p">:</span>
            <span class="c1"># Reraise if it&#39;s a missing custom template.</span>
            <span class="k">raise</span>
        <span class="k">return</span> <span class="n">HttpResponseServerError</span><span class="p">(</span>
            <span class="n">ERROR_PAGE_TEMPLATE</span> <span class="o">%</span> <span class="p">{</span><span class="s2">&quot;title&quot;</span><span class="p">:</span> <span class="s2">&quot;Server Error (500)&quot;</span><span class="p">,</span> <span class="s2">&quot;details&quot;</span><span class="p">:</span> <span class="s2">&quot;&quot;</span><span class="p">},</span>
        <span class="p">)</span>
    <span class="k">return</span> <span class="n">HttpResponseServerError</span><span class="p">(</span><span class="n">template</span><span class="o">.</span><span class="n">render</span><span class="p">())</span>


<span class="nd">@requires_csrf_token</span>
<span class="k">def</span><span class="w"> </span><span class="nf">bad_request</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">exception</span><span class="p">,</span> <span class="n">template_name</span><span class="o">=</span><span class="n">ERROR_400_TEMPLATE_NAME</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    400 error handler.</span>

<span class="sd">    Templates: :template:`400.html`</span>
<span class="sd">    Context: None</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">template</span> <span class="o">=</span> <span class="n">loader</span><span class="o">.</span><span class="n">get_template</span><span class="p">(</span><span class="n">template_name</span><span class="p">)</span>
        <span class="n">body</span> <span class="o">=</span> <span class="n">template</span><span class="o">.</span><span class="n">render</span><span class="p">(</span><span class="n">request</span><span class="o">=</span><span class="n">request</span><span class="p">)</span>
    <span class="k">except</span> <span class="n">TemplateDoesNotExist</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">template_name</span> <span class="o">!=</span> <span class="n">ERROR_400_TEMPLATE_NAME</span><span class="p">:</span>
            <span class="c1"># Reraise if it&#39;s a missing custom template.</span>
            <span class="k">raise</span>
        <span class="k">return</span> <span class="n">HttpResponseBadRequest</span><span class="p">(</span>
            <span class="n">ERROR_PAGE_TEMPLATE</span> <span class="o">%</span> <span class="p">{</span><span class="s2">&quot;title&quot;</span><span class="p">:</span> <span class="s2">&quot;Bad Request (400)&quot;</span><span class="p">,</span> <span class="s2">&quot;details&quot;</span><span class="p">:</span> <span class="s2">&quot;&quot;</span><span class="p">},</span>
        <span class="p">)</span>
    <span class="c1"># No exception content is passed to the template, to not disclose any</span>
    <span class="c1"># sensitive information.</span>
    <span class="k">return</span> <span class="n">HttpResponseBadRequest</span><span class="p">(</span><span class="n">body</span><span class="p">)</span>


<span class="nd">@requires_csrf_token</span>
<span class="k">def</span><span class="w"> </span><span class="nf">permission_denied</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">exception</span><span class="p">,</span> <span class="n">template_name</span><span class="o">=</span><span class="n">ERROR_403_TEMPLATE_NAME</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Permission denied (403) handler.</span>

<span class="sd">    Templates: :template:`403.html`</span>
<span class="sd">    Context:</span>
<span class="sd">        exception</span>
<span class="sd">            The message from the exception which triggered the 403 (if one was</span>
<span class="sd">            supplied).</span>

<span class="sd">    If the template does not exist, an Http403 response containing the text</span>
<span class="sd">    &quot;403 Forbidden&quot; (as per RFC 9110 Section 15.5.4) will be returned.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">template</span> <span class="o">=</span> <span class="n">loader</span><span class="o">.</span><span class="n">get_template</span><span class="p">(</span><span class="n">template_name</span><span class="p">)</span>
    <span class="k">except</span> <span class="n">TemplateDoesNotExist</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">template_name</span> <span class="o">!=</span> <span class="n">ERROR_403_TEMPLATE_NAME</span><span class="p">:</span>
            <span class="c1"># Reraise if it&#39;s a missing custom template.</span>
            <span class="k">raise</span>
        <span class="k">return</span> <span class="n">HttpResponseForbidden</span><span class="p">(</span>
            <span class="n">ERROR_PAGE_TEMPLATE</span> <span class="o">%</span> <span class="p">{</span><span class="s2">&quot;title&quot;</span><span class="p">:</span> <span class="s2">&quot;403 Forbidden&quot;</span><span class="p">,</span> <span class="s2">&quot;details&quot;</span><span class="p">:</span> <span class="s2">&quot;&quot;</span><span class="p">},</span>
        <span class="p">)</span>
    <span class="k">return</span> <span class="n">HttpResponseForbidden</span><span class="p">(</span>
        <span class="n">template</span><span class="o">.</span><span class="n">render</span><span class="p">(</span><span class="n">request</span><span class="o">=</span><span class="n">request</span><span class="p">,</span> <span class="n">context</span><span class="o">=</span><span class="p">{</span><span class="s2">&quot;exception&quot;</span><span class="p">:</span> <span class="nb">str</span><span class="p">(</span><span class="n">exception</span><span class="p">)})</span>
    <span class="p">)</span>
</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>