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

<span class="kn">from</span> <span class="nn">django.http</span> <span class="k">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="nn">django.template</span> <span class="k">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="nn">django.views.decorators.csrf</span> <span class="k">import</span> <span class="n">requires_csrf_token</span>

<span class="n">ERROR_404_TEMPLATE_NAME</span> <span class="o">=</span> <span class="s1">&#39;404.html&#39;</span>
<span class="n">ERROR_403_TEMPLATE_NAME</span> <span class="o">=</span> <span class="s1">&#39;403.html&#39;</span>
<span class="n">ERROR_400_TEMPLATE_NAME</span> <span class="o">=</span> <span class="s1">&#39;400.html&#39;</span>
<span class="n">ERROR_500_TEMPLATE_NAME</span> <span class="o">=</span> <span class="s1">&#39;500.html&#39;</span>


<span class="c1"># This can be called when CsrfViewMiddleware.process_view has 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="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="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="s1">&#39;request_path&#39;</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="s1">&#39;exception&#39;</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="n">content_type</span> <span class="o">=</span> <span class="kc">None</span>             <span class="c1"># Django will use DEFAULT_CONTENT_TYPE</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="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="s1">&#39;&lt;h1&gt;Not Found&lt;/h1&gt;&#39;</span>
            <span class="s1">&#39;&lt;p&gt;The requested resource was not found on this server.&lt;/p&gt;&#39;</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="n">content_type</span> <span class="o">=</span> <span class="s1">&#39;text/html&#39;</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="n">content_type</span><span class="o">=</span><span class="n">content_type</span><span class="p">)</span>


<span class="nd">@requires_csrf_token</span>
<span class="k">def</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="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="s1">&#39;&lt;h1&gt;Server Error (500)&lt;/h1&gt;&#39;</span><span class="p">,</span> <span class="n">content_type</span><span class="o">=</span><span class="s1">&#39;text/html&#39;</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="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="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="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="s1">&#39;&lt;h1&gt;Bad Request (400)&lt;/h1&gt;&#39;</span><span class="p">,</span> <span class="n">content_type</span><span class="o">=</span><span class="s1">&#39;text/html&#39;</span><span class="p">)</span>
    <span class="c1"># No exception content is passed to the template, to not disclose any sensitive information.</span>
    <span class="k">return</span> <span class="n">HttpResponseBadRequest</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="c1"># This can be called when CsrfViewMiddleware.process_view has 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="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="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: None</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 7231) 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="s1">&#39;&lt;h1&gt;403 Forbidden&lt;/h1&gt;&#39;</span><span class="p">,</span> <span class="n">content_type</span><span class="o">=</span><span class="s1">&#39;text/html&#39;</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="s1">&#39;exception&#39;</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 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>