<!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.debug &#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-debug">
            
  <h1>Source code for django.views.debug</h1><div class="highlight"><pre>
<span></span><span class="kn">import</span><span class="w"> </span><span class="nn">functools</span>
<span class="kn">import</span><span class="w"> </span><span class="nn">inspect</span>
<span class="kn">import</span><span class="w"> </span><span class="nn">itertools</span>
<span class="kn">import</span><span class="w"> </span><span class="nn">re</span>
<span class="kn">import</span><span class="w"> </span><span class="nn">sys</span>
<span class="kn">import</span><span class="w"> </span><span class="nn">types</span>
<span class="kn">import</span><span class="w"> </span><span class="nn">warnings</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">pathlib</span><span class="w"> </span><span class="kn">import</span> <span class="n">Path</span>

<span class="kn">from</span><span class="w"> </span><span class="nn">django.conf</span><span class="w"> </span><span class="kn">import</span> <span class="n">settings</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="n">Http404</span><span class="p">,</span> <span class="n">HttpResponse</span><span class="p">,</span> <span class="n">HttpResponseNotFound</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="kn">from</span><span class="w"> </span><span class="nn">django.template.defaultfilters</span><span class="w"> </span><span class="kn">import</span> <span class="n">pprint</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">django.urls</span><span class="w"> </span><span class="kn">import</span> <span class="n">resolve</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">django.utils</span><span class="w"> </span><span class="kn">import</span> <span class="n">timezone</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">django.utils.datastructures</span><span class="w"> </span><span class="kn">import</span> <span class="n">MultiValueDict</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">django.utils.encoding</span><span class="w"> </span><span class="kn">import</span> <span class="n">force_str</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">django.utils.module_loading</span><span class="w"> </span><span class="kn">import</span> <span class="n">import_string</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">django.utils.regex_helper</span><span class="w"> </span><span class="kn">import</span> <span class="n">_lazy_re_compile</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">django.utils.version</span><span class="w"> </span><span class="kn">import</span> <span class="n">PY311</span><span class="p">,</span> <span class="n">get_docs_version</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">django.views.decorators.debug</span><span class="w"> </span><span class="kn">import</span> <span class="n">coroutine_functions_to_sensitive_variables</span>

<span class="c1"># Minimal Django templates engine to render the error templates</span>
<span class="c1"># regardless of the project&#39;s TEMPLATES setting. Templates are</span>
<span class="c1"># read directly from the filesystem so that the error handler</span>
<span class="c1"># works even if the template loader is broken.</span>
<span class="n">DEBUG_ENGINE</span> <span class="o">=</span> <span class="n">Engine</span><span class="p">(</span>
    <span class="n">debug</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
    <span class="n">libraries</span><span class="o">=</span><span class="p">{</span><span class="s2">&quot;i18n&quot;</span><span class="p">:</span> <span class="s2">&quot;django.templatetags.i18n&quot;</span><span class="p">},</span>
<span class="p">)</span>


<span class="k">def</span><span class="w"> </span><span class="nf">builtin_template_path</span><span class="p">(</span><span class="n">name</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Return a path to a builtin template.</span>

<span class="sd">    Avoid calling this function at the module level or in a class-definition</span>
<span class="sd">    because __file__ may not exist, e.g. in frozen environments.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">Path</span><span class="p">(</span><span class="vm">__file__</span><span class="p">)</span><span class="o">.</span><span class="n">parent</span> <span class="o">/</span> <span class="s2">&quot;templates&quot;</span> <span class="o">/</span> <span class="n">name</span>


<span class="k">class</span><span class="w"> </span><span class="nc">ExceptionCycleWarning</span><span class="p">(</span><span class="ne">UserWarning</span><span class="p">):</span>
    <span class="k">pass</span>


<span class="k">class</span><span class="w"> </span><span class="nc">CallableSettingWrapper</span><span class="p">:</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Object to wrap callable appearing in settings.</span>
<span class="sd">    * Not to call in the debug page (#21345).</span>
<span class="sd">    * Not to break the debug page if the callable forbidding to set attributes</span>
<span class="sd">      (#23070).</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span><span class="w"> </span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">callable_setting</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_wrapped</span> <span class="o">=</span> <span class="n">callable_setting</span>

    <span class="k">def</span><span class="w"> </span><span class="fm">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">repr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_wrapped</span><span class="p">)</span>


<span class="k">def</span><span class="w"> </span><span class="nf">technical_500_response</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">exc_type</span><span class="p">,</span> <span class="n">exc_value</span><span class="p">,</span> <span class="n">tb</span><span class="p">,</span> <span class="n">status_code</span><span class="o">=</span><span class="mi">500</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Create a technical server error response. The last three arguments are</span>
<span class="sd">    the values returned from sys.exc_info() and friends.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">reporter</span> <span class="o">=</span> <span class="n">get_exception_reporter_class</span><span class="p">(</span><span class="n">request</span><span class="p">)(</span><span class="n">request</span><span class="p">,</span> <span class="n">exc_type</span><span class="p">,</span> <span class="n">exc_value</span><span class="p">,</span> <span class="n">tb</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">request</span><span class="o">.</span><span class="n">accepts</span><span class="p">(</span><span class="s2">&quot;text/html&quot;</span><span class="p">):</span>
        <span class="n">html</span> <span class="o">=</span> <span class="n">reporter</span><span class="o">.</span><span class="n">get_traceback_html</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">HttpResponse</span><span class="p">(</span><span class="n">html</span><span class="p">,</span> <span class="n">status</span><span class="o">=</span><span class="n">status_code</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">text</span> <span class="o">=</span> <span class="n">reporter</span><span class="o">.</span><span class="n">get_traceback_text</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">HttpResponse</span><span class="p">(</span>
            <span class="n">text</span><span class="p">,</span> <span class="n">status</span><span class="o">=</span><span class="n">status_code</span><span class="p">,</span> <span class="n">content_type</span><span class="o">=</span><span class="s2">&quot;text/plain; charset=utf-8&quot;</span>
        <span class="p">)</span>


<span class="nd">@functools</span><span class="o">.</span><span class="n">lru_cache</span>
<span class="k">def</span><span class="w"> </span><span class="nf">get_default_exception_reporter_filter</span><span class="p">():</span>
    <span class="c1"># Instantiate the default filter for the first time and cache it.</span>
    <span class="k">return</span> <span class="n">import_string</span><span class="p">(</span><span class="n">settings</span><span class="o">.</span><span class="n">DEFAULT_EXCEPTION_REPORTER_FILTER</span><span class="p">)()</span>


<span class="k">def</span><span class="w"> </span><span class="nf">get_exception_reporter_filter</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="n">default_filter</span> <span class="o">=</span> <span class="n">get_default_exception_reporter_filter</span><span class="p">()</span>
    <span class="k">return</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="s2">&quot;exception_reporter_filter&quot;</span><span class="p">,</span> <span class="n">default_filter</span><span class="p">)</span>


<span class="k">def</span><span class="w"> </span><span class="nf">get_exception_reporter_class</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="n">default_exception_reporter_class</span> <span class="o">=</span> <span class="n">import_string</span><span class="p">(</span>
        <span class="n">settings</span><span class="o">.</span><span class="n">DEFAULT_EXCEPTION_REPORTER</span>
    <span class="p">)</span>
    <span class="k">return</span> <span class="nb">getattr</span><span class="p">(</span>
        <span class="n">request</span><span class="p">,</span> <span class="s2">&quot;exception_reporter_class&quot;</span><span class="p">,</span> <span class="n">default_exception_reporter_class</span>
    <span class="p">)</span>


<span class="k">def</span><span class="w"> </span><span class="nf">get_caller</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="n">resolver_match</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">resolver_match</span>
    <span class="k">if</span> <span class="n">resolver_match</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">resolver_match</span> <span class="o">=</span> <span class="n">resolve</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="k">except</span> <span class="n">Http404</span><span class="p">:</span>
            <span class="k">pass</span>
    <span class="k">return</span> <span class="s2">&quot;&quot;</span> <span class="k">if</span> <span class="n">resolver_match</span> <span class="ow">is</span> <span class="kc">None</span> <span class="k">else</span> <span class="n">resolver_match</span><span class="o">.</span><span class="n">_func_path</span>


<div class="viewcode-block" id="SafeExceptionReporterFilter">
<a class="viewcode-back" href="../../../howto/error-reporting.html#django.views.debug.SafeExceptionReporterFilter">[docs]</a>
<span class="k">class</span><span class="w"> </span><span class="nc">SafeExceptionReporterFilter</span><span class="p">:</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Use annotations made by the sensitive_post_parameters and</span>
<span class="sd">    sensitive_variables decorators to filter out sensitive information.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">cleansed_substitute</span> <span class="o">=</span> <span class="s2">&quot;********************&quot;</span>
    <span class="n">hidden_settings</span> <span class="o">=</span> <span class="n">_lazy_re_compile</span><span class="p">(</span>
        <span class="s2">&quot;API|AUTH|TOKEN|KEY|SECRET|PASS|SIGNATURE|HTTP_COOKIE&quot;</span><span class="p">,</span> <span class="n">flags</span><span class="o">=</span><span class="n">re</span><span class="o">.</span><span class="n">I</span>
    <span class="p">)</span>

    <span class="k">def</span><span class="w"> </span><span class="nf">cleanse_setting</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Cleanse an individual setting key/value of sensitive content. If the</span>
<span class="sd">        value is a dictionary, recursively cleanse the keys in that dictionary.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">key</span> <span class="o">==</span> <span class="n">settings</span><span class="o">.</span><span class="n">SESSION_COOKIE_NAME</span><span class="p">:</span>
            <span class="n">is_sensitive</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">is_sensitive</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">hidden_settings</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
            <span class="k">except</span> <span class="ne">TypeError</span><span class="p">:</span>
                <span class="n">is_sensitive</span> <span class="o">=</span> <span class="kc">False</span>

        <span class="k">if</span> <span class="n">is_sensitive</span><span class="p">:</span>
            <span class="n">cleansed</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">cleansed_substitute</span>
        <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="nb">dict</span><span class="p">):</span>
            <span class="n">cleansed</span> <span class="o">=</span> <span class="p">{</span><span class="n">k</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">cleanse_setting</span><span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span> <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">value</span><span class="o">.</span><span class="n">items</span><span class="p">()}</span>
        <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="nb">list</span><span class="p">):</span>
            <span class="n">cleansed</span> <span class="o">=</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">cleanse_setting</span><span class="p">(</span><span class="s2">&quot;&quot;</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span> <span class="k">for</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">value</span><span class="p">]</span>
        <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">):</span>
            <span class="n">cleansed</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">([</span><span class="bp">self</span><span class="o">.</span><span class="n">cleanse_setting</span><span class="p">(</span><span class="s2">&quot;&quot;</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span> <span class="k">for</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">value</span><span class="p">])</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">cleansed</span> <span class="o">=</span> <span class="n">value</span>

        <span class="k">if</span> <span class="nb">callable</span><span class="p">(</span><span class="n">cleansed</span><span class="p">):</span>
            <span class="n">cleansed</span> <span class="o">=</span> <span class="n">CallableSettingWrapper</span><span class="p">(</span><span class="n">cleansed</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">cleansed</span>

    <span class="k">def</span><span class="w"> </span><span class="nf">get_safe_settings</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return a dictionary of the settings module with values of sensitive</span>
<span class="sd">        settings replaced with stars (*********).</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">settings_dict</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="nb">dir</span><span class="p">(</span><span class="n">settings</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">k</span><span class="o">.</span><span class="n">isupper</span><span class="p">():</span>
                <span class="n">settings_dict</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">cleanse_setting</span><span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">settings</span><span class="p">,</span> <span class="n">k</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">settings_dict</span>

    <span class="k">def</span><span class="w"> </span><span class="nf">get_safe_request_meta</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return a dictionary of request.META with sensitive values redacted.</span>
<span class="sd">        &quot;&quot;&quot;</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">return</span> <span class="p">{}</span>
        <span class="k">return</span> <span class="p">{</span><span class="n">k</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">cleanse_setting</span><span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span> <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">request</span><span class="o">.</span><span class="n">META</span><span class="o">.</span><span class="n">items</span><span class="p">()}</span>

    <span class="k">def</span><span class="w"> </span><span class="nf">get_safe_cookies</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return a dictionary of request.COOKIES with sensitive values redacted.</span>
<span class="sd">        &quot;&quot;&quot;</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;COOKIES&quot;</span><span class="p">):</span>
            <span class="k">return</span> <span class="p">{}</span>
        <span class="k">return</span> <span class="p">{</span><span class="n">k</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">cleanse_setting</span><span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span> <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">request</span><span class="o">.</span><span class="n">COOKIES</span><span class="o">.</span><span class="n">items</span><span class="p">()}</span>

<div class="viewcode-block" id="SafeExceptionReporterFilter.is_active">
<a class="viewcode-back" href="../../../howto/error-reporting.html#django.views.debug.SafeExceptionReporterFilter.is_active">[docs]</a>
    <span class="k">def</span><span class="w"> </span><span class="nf">is_active</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        This filter is to add safety in production environments (i.e. DEBUG</span>
<span class="sd">        is False). If DEBUG is True then your site is not safe anyway.</span>
<span class="sd">        This hook is provided as a convenience to easily activate or</span>
<span class="sd">        deactivate the filter on a per request basis.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">settings</span><span class="o">.</span><span class="n">DEBUG</span> <span class="ow">is</span> <span class="kc">False</span></div>


    <span class="k">def</span><span class="w"> </span><span class="nf">get_cleansed_multivaluedict</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">multivaluedict</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Replace the keys in a MultiValueDict marked as sensitive with stars.</span>
<span class="sd">        This mitigates leaking sensitive POST parameters if something like</span>
<span class="sd">        request.POST[&#39;nonexistent_key&#39;] throws an exception (#21098).</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">sensitive_post_parameters</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="s2">&quot;sensitive_post_parameters&quot;</span><span class="p">,</span> <span class="p">[])</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_active</span><span class="p">(</span><span class="n">request</span><span class="p">)</span> <span class="ow">and</span> <span class="n">sensitive_post_parameters</span><span class="p">:</span>
            <span class="n">multivaluedict</span> <span class="o">=</span> <span class="n">multivaluedict</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
            <span class="k">for</span> <span class="n">param</span> <span class="ow">in</span> <span class="n">sensitive_post_parameters</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">param</span> <span class="ow">in</span> <span class="n">multivaluedict</span><span class="p">:</span>
                    <span class="n">multivaluedict</span><span class="p">[</span><span class="n">param</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">cleansed_substitute</span>
        <span class="k">return</span> <span class="n">multivaluedict</span>

<div class="viewcode-block" id="SafeExceptionReporterFilter.get_post_parameters">
<a class="viewcode-back" href="../../../howto/error-reporting.html#django.views.debug.SafeExceptionReporterFilter.get_post_parameters">[docs]</a>
    <span class="k">def</span><span class="w"> </span><span class="nf">get_post_parameters</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Replace the values of POST parameters marked as sensitive with</span>
<span class="sd">        stars (*********).</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">request</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">{}</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">sensitive_post_parameters</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span>
                <span class="n">request</span><span class="p">,</span> <span class="s2">&quot;sensitive_post_parameters&quot;</span><span class="p">,</span> <span class="p">[]</span>
            <span class="p">)</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_active</span><span class="p">(</span><span class="n">request</span><span class="p">)</span> <span class="ow">and</span> <span class="n">sensitive_post_parameters</span><span class="p">:</span>
                <span class="n">cleansed</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">POST</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
                <span class="k">if</span> <span class="n">sensitive_post_parameters</span> <span class="o">==</span> <span class="s2">&quot;__ALL__&quot;</span><span class="p">:</span>
                    <span class="c1"># Cleanse all parameters.</span>
                    <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">cleansed</span><span class="p">:</span>
                        <span class="n">cleansed</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">cleansed_substitute</span>
                    <span class="k">return</span> <span class="n">cleansed</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="c1"># Cleanse only the specified parameters.</span>
                    <span class="k">for</span> <span class="n">param</span> <span class="ow">in</span> <span class="n">sensitive_post_parameters</span><span class="p">:</span>
                        <span class="k">if</span> <span class="n">param</span> <span class="ow">in</span> <span class="n">cleansed</span><span class="p">:</span>
                            <span class="n">cleansed</span><span class="p">[</span><span class="n">param</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">cleansed_substitute</span>
                    <span class="k">return</span> <span class="n">cleansed</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">return</span> <span class="n">request</span><span class="o">.</span><span class="n">POST</span></div>


    <span class="k">def</span><span class="w"> </span><span class="nf">cleanse_special_types</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="c1"># If value is lazy or a complex object of another kind, this check</span>
            <span class="c1"># might raise an exception. isinstance checks that lazy</span>
            <span class="c1"># MultiValueDicts will have a return value.</span>
            <span class="n">is_multivalue_dict</span> <span class="o">=</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">MultiValueDict</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
            <span class="k">return</span> <span class="s2">&quot;</span><span class="si">{!r}</span><span class="s2"> while evaluating </span><span class="si">{!r}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">e</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">is_multivalue_dict</span><span class="p">:</span>
            <span class="c1"># Cleanse MultiValueDicts (request.POST is the one we usually care about)</span>
            <span class="n">value</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_cleansed_multivaluedict</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">value</span>

<div class="viewcode-block" id="SafeExceptionReporterFilter.get_traceback_frame_variables">
<a class="viewcode-back" href="../../../howto/error-reporting.html#django.views.debug.SafeExceptionReporterFilter.get_traceback_frame_variables">[docs]</a>
    <span class="k">def</span><span class="w"> </span><span class="nf">get_traceback_frame_variables</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">tb_frame</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Replace the values of variables marked as sensitive with</span>
<span class="sd">        stars (*********).</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">sensitive_variables</span> <span class="o">=</span> <span class="kc">None</span>

        <span class="c1"># Coroutines don&#39;t have a proper `f_back` so they need to be inspected</span>
        <span class="c1"># separately. Handle this by stashing the registered sensitive</span>
        <span class="c1"># variables in a global dict indexed by `hash(file_path:line_number)`.</span>
        <span class="k">if</span> <span class="p">(</span>
            <span class="n">tb_frame</span><span class="o">.</span><span class="n">f_code</span><span class="o">.</span><span class="n">co_flags</span> <span class="o">&amp;</span> <span class="n">inspect</span><span class="o">.</span><span class="n">CO_COROUTINE</span> <span class="o">!=</span> <span class="mi">0</span>
            <span class="ow">and</span> <span class="n">tb_frame</span><span class="o">.</span><span class="n">f_code</span><span class="o">.</span><span class="n">co_name</span> <span class="o">!=</span> <span class="s2">&quot;sensitive_variables_wrapper&quot;</span>
        <span class="p">):</span>
            <span class="n">key</span> <span class="o">=</span> <span class="nb">hash</span><span class="p">(</span>
                <span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">tb_frame</span><span class="o">.</span><span class="n">f_code</span><span class="o">.</span><span class="n">co_filename</span><span class="si">}</span><span class="s2">:</span><span class="si">{</span><span class="n">tb_frame</span><span class="o">.</span><span class="n">f_code</span><span class="o">.</span><span class="n">co_firstlineno</span><span class="si">}</span><span class="s2">&quot;</span>
            <span class="p">)</span>
            <span class="n">sensitive_variables</span> <span class="o">=</span> <span class="n">coroutine_functions_to_sensitive_variables</span><span class="o">.</span><span class="n">get</span><span class="p">(</span>
                <span class="n">key</span><span class="p">,</span> <span class="kc">None</span>
            <span class="p">)</span>

        <span class="k">if</span> <span class="n">sensitive_variables</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="c1"># Loop through the frame&#39;s callers to see if the</span>
            <span class="c1"># sensitive_variables decorator was used.</span>
            <span class="n">current_frame</span> <span class="o">=</span> <span class="n">tb_frame</span>
            <span class="k">while</span> <span class="n">current_frame</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="k">if</span> <span class="p">(</span>
                    <span class="n">current_frame</span><span class="o">.</span><span class="n">f_code</span><span class="o">.</span><span class="n">co_name</span> <span class="o">==</span> <span class="s2">&quot;sensitive_variables_wrapper&quot;</span>
                    <span class="ow">and</span> <span class="s2">&quot;sensitive_variables_wrapper&quot;</span> <span class="ow">in</span> <span class="n">current_frame</span><span class="o">.</span><span class="n">f_locals</span>
                <span class="p">):</span>
                    <span class="c1"># The sensitive_variables decorator was used, so take note</span>
                    <span class="c1"># of the sensitive variables&#39; names.</span>
                    <span class="n">wrapper</span> <span class="o">=</span> <span class="n">current_frame</span><span class="o">.</span><span class="n">f_locals</span><span class="p">[</span><span class="s2">&quot;sensitive_variables_wrapper&quot;</span><span class="p">]</span>
                    <span class="n">sensitive_variables</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">wrapper</span><span class="p">,</span> <span class="s2">&quot;sensitive_variables&quot;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
                    <span class="k">break</span>
                <span class="n">current_frame</span> <span class="o">=</span> <span class="n">current_frame</span><span class="o">.</span><span class="n">f_back</span>

        <span class="n">cleansed</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_active</span><span class="p">(</span><span class="n">request</span><span class="p">)</span> <span class="ow">and</span> <span class="n">sensitive_variables</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">sensitive_variables</span> <span class="o">==</span> <span class="s2">&quot;__ALL__&quot;</span><span class="p">:</span>
                <span class="c1"># Cleanse all variables</span>
                <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">tb_frame</span><span class="o">.</span><span class="n">f_locals</span><span class="p">:</span>
                    <span class="n">cleansed</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">cleansed_substitute</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="c1"># Cleanse specified variables</span>
                <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">tb_frame</span><span class="o">.</span><span class="n">f_locals</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                    <span class="k">if</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">sensitive_variables</span><span class="p">:</span>
                        <span class="n">value</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">cleansed_substitute</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="n">value</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">cleanse_special_types</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
                    <span class="n">cleansed</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># Potentially cleanse the request and any MultiValueDicts if they</span>
            <span class="c1"># are one of the frame variables.</span>
            <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">tb_frame</span><span class="o">.</span><span class="n">f_locals</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                <span class="n">cleansed</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">cleanse_special_types</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>

        <span class="k">if</span> <span class="p">(</span>
            <span class="n">tb_frame</span><span class="o">.</span><span class="n">f_code</span><span class="o">.</span><span class="n">co_name</span> <span class="o">==</span> <span class="s2">&quot;sensitive_variables_wrapper&quot;</span>
            <span class="ow">and</span> <span class="s2">&quot;sensitive_variables_wrapper&quot;</span> <span class="ow">in</span> <span class="n">tb_frame</span><span class="o">.</span><span class="n">f_locals</span>
        <span class="p">):</span>
            <span class="c1"># For good measure, obfuscate the decorated function&#39;s arguments in</span>
            <span class="c1"># the sensitive_variables decorator&#39;s frame, in case the variables</span>
            <span class="c1"># associated with those arguments were meant to be obfuscated from</span>
            <span class="c1"># the decorated function&#39;s frame.</span>
            <span class="n">cleansed</span><span class="p">[</span><span class="s2">&quot;func_args&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">cleansed_substitute</span>
            <span class="n">cleansed</span><span class="p">[</span><span class="s2">&quot;func_kwargs&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">cleansed_substitute</span>

        <span class="k">return</span> <span class="n">cleansed</span><span class="o">.</span><span class="n">items</span><span class="p">()</span></div>
</div>



<div class="viewcode-block" id="ExceptionReporter">
<a class="viewcode-back" href="../../../howto/error-reporting.html#django.views.debug.ExceptionReporter">[docs]</a>
<span class="k">class</span><span class="w"> </span><span class="nc">ExceptionReporter</span><span class="p">:</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;Organize and coordinate reporting on exceptions.&quot;&quot;&quot;</span>

    <span class="nd">@property</span>
    <span class="k">def</span><span class="w"> </span><span class="nf">html_template_path</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">builtin_template_path</span><span class="p">(</span><span class="s2">&quot;technical_500.html&quot;</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span><span class="w"> </span><span class="nf">text_template_path</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">builtin_template_path</span><span class="p">(</span><span class="s2">&quot;technical_500.txt&quot;</span><span class="p">)</span>

    <span class="k">def</span><span class="w"> </span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">exc_type</span><span class="p">,</span> <span class="n">exc_value</span><span class="p">,</span> <span class="n">tb</span><span class="p">,</span> <span class="n">is_email</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">request</span> <span class="o">=</span> <span class="n">request</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">filter</span> <span class="o">=</span> <span class="n">get_exception_reporter_filter</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">request</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">exc_type</span> <span class="o">=</span> <span class="n">exc_type</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">exc_value</span> <span class="o">=</span> <span class="n">exc_value</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">tb</span> <span class="o">=</span> <span class="n">tb</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">is_email</span> <span class="o">=</span> <span class="n">is_email</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">template_info</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">exc_value</span><span class="p">,</span> <span class="s2">&quot;template_debug&quot;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">template_does_not_exist</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">postmortem</span> <span class="o">=</span> <span class="kc">None</span>

    <span class="k">def</span><span class="w"> </span><span class="nf">_get_raw_insecure_uri</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return an absolute URI from variables available in this request. Skip</span>
<span class="sd">        allowed hosts protection, so may return insecure URI.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="s2">&quot;</span><span class="si">{scheme}</span><span class="s2">://</span><span class="si">{host}{path}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
            <span class="n">scheme</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">scheme</span><span class="p">,</span>
            <span class="n">host</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">_get_raw_host</span><span class="p">(),</span>
            <span class="n">path</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">get_full_path</span><span class="p">(),</span>
        <span class="p">)</span>

<div class="viewcode-block" id="ExceptionReporter.get_traceback_data">
<a class="viewcode-back" href="../../../howto/error-reporting.html#django.views.debug.ExceptionReporter.get_traceback_data">[docs]</a>
    <span class="k">def</span><span class="w"> </span><span class="nf">get_traceback_data</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;Return a dictionary containing traceback information.&quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">exc_type</span> <span class="ow">and</span> <span class="nb">issubclass</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">exc_type</span><span class="p">,</span> <span class="n">TemplateDoesNotExist</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">template_does_not_exist</span> <span class="o">=</span> <span class="kc">True</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">postmortem</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">exc_value</span><span class="o">.</span><span class="n">chain</span> <span class="ow">or</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">exc_value</span><span class="p">]</span>

        <span class="n">frames</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_traceback_frames</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">frame</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">frames</span><span class="p">):</span>
            <span class="k">if</span> <span class="s2">&quot;vars&quot;</span> <span class="ow">in</span> <span class="n">frame</span><span class="p">:</span>
                <span class="n">frame_vars</span> <span class="o">=</span> <span class="p">[]</span>
                <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">frame</span><span class="p">[</span><span class="s2">&quot;vars&quot;</span><span class="p">]:</span>
                    <span class="n">v</span> <span class="o">=</span> <span class="n">pprint</span><span class="p">(</span><span class="n">v</span><span class="p">)</span>
                    <span class="c1"># Trim large blobs of data</span>
                    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">v</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">4096</span><span class="p">:</span>
                        <span class="n">v</span> <span class="o">=</span> <span class="s2">&quot;</span><span class="si">%s</span><span class="s2">… &lt;trimmed </span><span class="si">%d</span><span class="s2"> bytes string&gt;&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">v</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">4096</span><span class="p">],</span> <span class="nb">len</span><span class="p">(</span><span class="n">v</span><span class="p">))</span>
                    <span class="n">frame_vars</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">k</span><span class="p">,</span> <span class="n">v</span><span class="p">))</span>
                <span class="n">frame</span><span class="p">[</span><span class="s2">&quot;vars&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">frame_vars</span>
            <span class="n">frames</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">frame</span>

        <span class="n">unicode_hint</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">exc_type</span> <span class="ow">and</span> <span class="nb">issubclass</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">exc_type</span><span class="p">,</span> <span class="ne">UnicodeError</span><span class="p">):</span>
            <span class="n">start</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">exc_value</span><span class="p">,</span> <span class="s2">&quot;start&quot;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
            <span class="n">end</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">exc_value</span><span class="p">,</span> <span class="s2">&quot;end&quot;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">start</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">end</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">unicode_str</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">exc_value</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
                <span class="n">unicode_hint</span> <span class="o">=</span> <span class="n">force_str</span><span class="p">(</span>
                    <span class="n">unicode_str</span><span class="p">[</span><span class="nb">max</span><span class="p">(</span><span class="n">start</span> <span class="o">-</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span> <span class="p">:</span> <span class="nb">min</span><span class="p">(</span><span class="n">end</span> <span class="o">+</span> <span class="mi">5</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">unicode_str</span><span class="p">))],</span>
                    <span class="s2">&quot;ascii&quot;</span><span class="p">,</span>
                    <span class="n">errors</span><span class="o">=</span><span class="s2">&quot;replace&quot;</span><span class="p">,</span>
                <span class="p">)</span>
        <span class="kn">from</span><span class="w"> </span><span class="nn">django</span><span class="w"> </span><span class="kn">import</span> <span class="n">get_version</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">request</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">user_str</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">user_str</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">user</span><span class="p">)</span>
            <span class="k">except</span> <span class="ne">Exception</span><span class="p">:</span>
                <span class="c1"># request.user may raise OperationalError if the database is</span>
                <span class="c1"># unavailable, for example.</span>
                <span class="n">user_str</span> <span class="o">=</span> <span class="s2">&quot;[unable to retrieve the current user]&quot;</span>

        <span class="n">c</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s2">&quot;is_email&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_email</span><span class="p">,</span>
            <span class="s2">&quot;unicode_hint&quot;</span><span class="p">:</span> <span class="n">unicode_hint</span><span class="p">,</span>
            <span class="s2">&quot;frames&quot;</span><span class="p">:</span> <span class="n">frames</span><span class="p">,</span>
            <span class="s2">&quot;request&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">request</span><span class="p">,</span>
            <span class="s2">&quot;request_meta&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">filter</span><span class="o">.</span><span class="n">get_safe_request_meta</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">request</span><span class="p">),</span>
            <span class="s2">&quot;request_COOKIES_items&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">filter</span><span class="o">.</span><span class="n">get_safe_cookies</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">request</span><span class="p">)</span><span class="o">.</span><span class="n">items</span><span class="p">(),</span>
            <span class="s2">&quot;user_str&quot;</span><span class="p">:</span> <span class="n">user_str</span><span class="p">,</span>
            <span class="s2">&quot;filtered_POST_items&quot;</span><span class="p">:</span> <span class="nb">list</span><span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">filter</span><span class="o">.</span><span class="n">get_post_parameters</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">request</span><span class="p">)</span><span class="o">.</span><span class="n">items</span><span class="p">()</span>
            <span class="p">),</span>
            <span class="s2">&quot;settings&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">filter</span><span class="o">.</span><span class="n">get_safe_settings</span><span class="p">(),</span>
            <span class="s2">&quot;sys_executable&quot;</span><span class="p">:</span> <span class="n">sys</span><span class="o">.</span><span class="n">executable</span><span class="p">,</span>
            <span class="s2">&quot;sys_version_info&quot;</span><span class="p">:</span> <span class="s2">&quot;</span><span class="si">%d</span><span class="s2">.</span><span class="si">%d</span><span class="s2">.</span><span class="si">%d</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">sys</span><span class="o">.</span><span class="n">version_info</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">3</span><span class="p">],</span>
            <span class="s2">&quot;server_time&quot;</span><span class="p">:</span> <span class="n">timezone</span><span class="o">.</span><span class="n">now</span><span class="p">(),</span>
            <span class="s2">&quot;django_version_info&quot;</span><span class="p">:</span> <span class="n">get_version</span><span class="p">(),</span>
            <span class="s2">&quot;sys_path&quot;</span><span class="p">:</span> <span class="n">sys</span><span class="o">.</span><span class="n">path</span><span class="p">,</span>
            <span class="s2">&quot;template_info&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">template_info</span><span class="p">,</span>
            <span class="s2">&quot;template_does_not_exist&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">template_does_not_exist</span><span class="p">,</span>
            <span class="s2">&quot;postmortem&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">postmortem</span><span class="p">,</span>
        <span class="p">}</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">request</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">c</span><span class="p">[</span><span class="s2">&quot;request_GET_items&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">GET</span><span class="o">.</span><span class="n">items</span><span class="p">()</span>
            <span class="n">c</span><span class="p">[</span><span class="s2">&quot;request_FILES_items&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">FILES</span><span class="o">.</span><span class="n">items</span><span class="p">()</span>
            <span class="n">c</span><span class="p">[</span><span class="s2">&quot;request_insecure_uri&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_raw_insecure_uri</span><span class="p">()</span>
            <span class="n">c</span><span class="p">[</span><span class="s2">&quot;raising_view_name&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">get_caller</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">request</span><span class="p">)</span>

        <span class="c1"># Check whether exception info is available</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">exc_type</span><span class="p">:</span>
            <span class="n">c</span><span class="p">[</span><span class="s2">&quot;exception_type&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">exc_type</span><span class="o">.</span><span class="vm">__name__</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">exc_value</span><span class="p">:</span>
            <span class="n">c</span><span class="p">[</span><span class="s2">&quot;exception_value&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">exc_value</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">exc_notes</span> <span class="o">:=</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">exc_value</span><span class="p">,</span> <span class="s2">&quot;__notes__&quot;</span><span class="p">,</span> <span class="kc">None</span><span class="p">):</span>
                <span class="n">c</span><span class="p">[</span><span class="s2">&quot;exception_notes&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span> <span class="o">+</span> <span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">exc_notes</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">frames</span><span class="p">:</span>
            <span class="n">c</span><span class="p">[</span><span class="s2">&quot;lastframe&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">frames</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
        <span class="k">return</span> <span class="n">c</span></div>


<div class="viewcode-block" id="ExceptionReporter.get_traceback_html">
<a class="viewcode-back" href="../../../howto/error-reporting.html#django.views.debug.ExceptionReporter.get_traceback_html">[docs]</a>
    <span class="k">def</span><span class="w"> </span><span class="nf">get_traceback_html</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;Return HTML version of debug 500 HTTP error page.&quot;&quot;&quot;</span>
        <span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">html_template_path</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">encoding</span><span class="o">=</span><span class="s2">&quot;utf-8&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">fh</span><span class="p">:</span>
            <span class="n">t</span> <span class="o">=</span> <span class="n">DEBUG_ENGINE</span><span class="o">.</span><span class="n">from_string</span><span class="p">(</span><span class="n">fh</span><span class="o">.</span><span class="n">read</span><span class="p">())</span>
        <span class="n">c</span> <span class="o">=</span> <span class="n">Context</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">get_traceback_data</span><span class="p">(),</span> <span class="n">use_l10n</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">t</span><span class="o">.</span><span class="n">render</span><span class="p">(</span><span class="n">c</span><span class="p">)</span></div>


<div class="viewcode-block" id="ExceptionReporter.get_traceback_text">
<a class="viewcode-back" href="../../../howto/error-reporting.html#django.views.debug.ExceptionReporter.get_traceback_text">[docs]</a>
    <span class="k">def</span><span class="w"> </span><span class="nf">get_traceback_text</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;Return plain text version of debug 500 HTTP error page.&quot;&quot;&quot;</span>
        <span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">text_template_path</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">encoding</span><span class="o">=</span><span class="s2">&quot;utf-8&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">fh</span><span class="p">:</span>
            <span class="n">t</span> <span class="o">=</span> <span class="n">DEBUG_ENGINE</span><span class="o">.</span><span class="n">from_string</span><span class="p">(</span><span class="n">fh</span><span class="o">.</span><span class="n">read</span><span class="p">())</span>
        <span class="n">c</span> <span class="o">=</span> <span class="n">Context</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">get_traceback_data</span><span class="p">(),</span> <span class="n">autoescape</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">use_l10n</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">t</span><span class="o">.</span><span class="n">render</span><span class="p">(</span><span class="n">c</span><span class="p">)</span></div>


    <span class="k">def</span><span class="w"> </span><span class="nf">_get_source</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">filename</span><span class="p">,</span> <span class="n">loader</span><span class="p">,</span> <span class="n">module_name</span><span class="p">):</span>
        <span class="n">source</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">loader</span><span class="p">,</span> <span class="s2">&quot;get_source&quot;</span><span class="p">):</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">source</span> <span class="o">=</span> <span class="n">loader</span><span class="o">.</span><span class="n">get_source</span><span class="p">(</span><span class="n">module_name</span><span class="p">)</span>
            <span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
                <span class="k">pass</span>
            <span class="k">if</span> <span class="n">source</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">source</span> <span class="o">=</span> <span class="n">source</span><span class="o">.</span><span class="n">splitlines</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">source</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="s2">&quot;rb&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">fp</span><span class="p">:</span>
                    <span class="n">source</span> <span class="o">=</span> <span class="n">fp</span><span class="o">.</span><span class="n">read</span><span class="p">()</span><span class="o">.</span><span class="n">splitlines</span><span class="p">()</span>
            <span class="k">except</span> <span class="ne">OSError</span><span class="p">:</span>
                <span class="k">pass</span>
        <span class="k">return</span> <span class="n">source</span>

    <span class="k">def</span><span class="w"> </span><span class="nf">_get_lines_from_file</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span> <span class="n">filename</span><span class="p">,</span> <span class="n">lineno</span><span class="p">,</span> <span class="n">context_lines</span><span class="p">,</span> <span class="n">loader</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">module_name</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">        Return context_lines before and after lineno from file.</span>
<span class="sd">        Return (pre_context_lineno, pre_context, context_line, post_context).</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">source</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_source</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="n">loader</span><span class="p">,</span> <span class="n">module_name</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">source</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">None</span><span class="p">,</span> <span class="p">[],</span> <span class="kc">None</span><span class="p">,</span> <span class="p">[]</span>

        <span class="c1"># If we just read the source from a file, or if the loader did not</span>
        <span class="c1"># apply tokenize.detect_encoding to decode the source into a</span>
        <span class="c1"># string, then we should do that ourselves.</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">source</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="nb">bytes</span><span class="p">):</span>
            <span class="n">encoding</span> <span class="o">=</span> <span class="s2">&quot;ascii&quot;</span>
            <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">source</span><span class="p">[:</span><span class="mi">2</span><span class="p">]:</span>
                <span class="c1"># File coding may be specified. Match pattern from PEP-263</span>
                <span class="c1"># (https://www.python.org/dev/peps/pep-0263/)</span>
                <span class="n">match</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="sa">rb</span><span class="s2">&quot;coding[:=]\s*([-\w.]+)&quot;</span><span class="p">,</span> <span class="n">line</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">match</span><span class="p">:</span>
                    <span class="n">encoding</span> <span class="o">=</span> <span class="n">match</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s2">&quot;ascii&quot;</span><span class="p">)</span>
                    <span class="k">break</span>
            <span class="n">source</span> <span class="o">=</span> <span class="p">[</span><span class="nb">str</span><span class="p">(</span><span class="n">sline</span><span class="p">,</span> <span class="n">encoding</span><span class="p">,</span> <span class="s2">&quot;replace&quot;</span><span class="p">)</span> <span class="k">for</span> <span class="n">sline</span> <span class="ow">in</span> <span class="n">source</span><span class="p">]</span>

        <span class="n">lower_bound</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">lineno</span> <span class="o">-</span> <span class="n">context_lines</span><span class="p">)</span>
        <span class="n">upper_bound</span> <span class="o">=</span> <span class="n">lineno</span> <span class="o">+</span> <span class="n">context_lines</span>

        <span class="k">try</span><span class="p">:</span>
            <span class="n">pre_context</span> <span class="o">=</span> <span class="n">source</span><span class="p">[</span><span class="n">lower_bound</span><span class="p">:</span><span class="n">lineno</span><span class="p">]</span>
            <span class="n">context_line</span> <span class="o">=</span> <span class="n">source</span><span class="p">[</span><span class="n">lineno</span><span class="p">]</span>
            <span class="n">post_context</span> <span class="o">=</span> <span class="n">source</span><span class="p">[</span><span class="n">lineno</span> <span class="o">+</span> <span class="mi">1</span> <span class="p">:</span> <span class="n">upper_bound</span><span class="p">]</span>
        <span class="k">except</span> <span class="ne">IndexError</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">None</span><span class="p">,</span> <span class="p">[],</span> <span class="kc">None</span><span class="p">,</span> <span class="p">[]</span>
        <span class="k">return</span> <span class="n">lower_bound</span><span class="p">,</span> <span class="n">pre_context</span><span class="p">,</span> <span class="n">context_line</span><span class="p">,</span> <span class="n">post_context</span>

    <span class="k">def</span><span class="w"> </span><span class="nf">_get_explicit_or_implicit_cause</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">exc_value</span><span class="p">):</span>
        <span class="n">explicit</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">exc_value</span><span class="p">,</span> <span class="s2">&quot;__cause__&quot;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
        <span class="n">suppress_context</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">exc_value</span><span class="p">,</span> <span class="s2">&quot;__suppress_context__&quot;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
        <span class="n">implicit</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">exc_value</span><span class="p">,</span> <span class="s2">&quot;__context__&quot;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">explicit</span> <span class="ow">or</span> <span class="p">(</span><span class="kc">None</span> <span class="k">if</span> <span class="n">suppress_context</span> <span class="k">else</span> <span class="n">implicit</span><span class="p">)</span>

    <span class="k">def</span><span class="w"> </span><span class="nf">get_traceback_frames</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># Get the exception and all its causes</span>
        <span class="n">exceptions</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">exc_value</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">exc_value</span>
        <span class="k">while</span> <span class="n">exc_value</span><span class="p">:</span>
            <span class="n">exceptions</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">exc_value</span><span class="p">)</span>
            <span class="n">exc_value</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_explicit_or_implicit_cause</span><span class="p">(</span><span class="n">exc_value</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">exc_value</span> <span class="ow">in</span> <span class="n">exceptions</span><span class="p">:</span>
                <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span>
                    <span class="s2">&quot;Cycle in the exception chain detected: exception &#39;</span><span class="si">%s</span><span class="s2">&#39; &quot;</span>
                    <span class="s2">&quot;encountered again.&quot;</span> <span class="o">%</span> <span class="n">exc_value</span><span class="p">,</span>
                    <span class="n">ExceptionCycleWarning</span><span class="p">,</span>
                <span class="p">)</span>
                <span class="c1"># Avoid infinite loop if there&#39;s a cyclic reference (#29393).</span>
                <span class="k">break</span>

        <span class="n">frames</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="c1"># No exceptions were supplied to ExceptionReporter</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">exceptions</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">frames</span>

        <span class="c1"># In case there&#39;s just one exception, take the traceback from self.tb</span>
        <span class="n">exc_value</span> <span class="o">=</span> <span class="n">exceptions</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span>
        <span class="n">tb</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">tb</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">exceptions</span> <span class="k">else</span> <span class="n">exc_value</span><span class="o">.</span><span class="n">__traceback__</span>
        <span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
            <span class="n">frames</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">get_exception_traceback_frames</span><span class="p">(</span><span class="n">exc_value</span><span class="p">,</span> <span class="n">tb</span><span class="p">))</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">exc_value</span> <span class="o">=</span> <span class="n">exceptions</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span>
            <span class="k">except</span> <span class="ne">IndexError</span><span class="p">:</span>
                <span class="k">break</span>
            <span class="n">tb</span> <span class="o">=</span> <span class="n">exc_value</span><span class="o">.</span><span class="n">__traceback__</span>
        <span class="k">return</span> <span class="n">frames</span>

    <span class="k">def</span><span class="w"> </span><span class="nf">get_exception_traceback_frames</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">exc_value</span><span class="p">,</span> <span class="n">tb</span><span class="p">):</span>
        <span class="n">exc_cause</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_explicit_or_implicit_cause</span><span class="p">(</span><span class="n">exc_value</span><span class="p">)</span>
        <span class="n">exc_cause_explicit</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">exc_value</span><span class="p">,</span> <span class="s2">&quot;__cause__&quot;</span><span class="p">,</span> <span class="kc">True</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">tb</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">yield</span> <span class="p">{</span>
                <span class="s2">&quot;exc_cause&quot;</span><span class="p">:</span> <span class="n">exc_cause</span><span class="p">,</span>
                <span class="s2">&quot;exc_cause_explicit&quot;</span><span class="p">:</span> <span class="n">exc_cause_explicit</span><span class="p">,</span>
                <span class="s2">&quot;tb&quot;</span><span class="p">:</span> <span class="kc">None</span><span class="p">,</span>
                <span class="s2">&quot;type&quot;</span><span class="p">:</span> <span class="s2">&quot;user&quot;</span><span class="p">,</span>
            <span class="p">}</span>
        <span class="k">while</span> <span class="n">tb</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="c1"># Support for __traceback_hide__ which is used by a few libraries</span>
            <span class="c1"># to hide internal frames.</span>
            <span class="k">if</span> <span class="n">tb</span><span class="o">.</span><span class="n">tb_frame</span><span class="o">.</span><span class="n">f_locals</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;__traceback_hide__&quot;</span><span class="p">):</span>
                <span class="n">tb</span> <span class="o">=</span> <span class="n">tb</span><span class="o">.</span><span class="n">tb_next</span>
                <span class="k">continue</span>
            <span class="n">filename</span> <span class="o">=</span> <span class="n">tb</span><span class="o">.</span><span class="n">tb_frame</span><span class="o">.</span><span class="n">f_code</span><span class="o">.</span><span class="n">co_filename</span>
            <span class="n">function</span> <span class="o">=</span> <span class="n">tb</span><span class="o">.</span><span class="n">tb_frame</span><span class="o">.</span><span class="n">f_code</span><span class="o">.</span><span class="n">co_name</span>
            <span class="n">lineno</span> <span class="o">=</span> <span class="n">tb</span><span class="o">.</span><span class="n">tb_lineno</span> <span class="o">-</span> <span class="mi">1</span>
            <span class="n">loader</span> <span class="o">=</span> <span class="n">tb</span><span class="o">.</span><span class="n">tb_frame</span><span class="o">.</span><span class="n">f_globals</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;__loader__&quot;</span><span class="p">)</span>
            <span class="n">module_name</span> <span class="o">=</span> <span class="n">tb</span><span class="o">.</span><span class="n">tb_frame</span><span class="o">.</span><span class="n">f_globals</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;__name__&quot;</span><span class="p">)</span> <span class="ow">or</span> <span class="s2">&quot;&quot;</span>
            <span class="p">(</span>
                <span class="n">pre_context_lineno</span><span class="p">,</span>
                <span class="n">pre_context</span><span class="p">,</span>
                <span class="n">context_line</span><span class="p">,</span>
                <span class="n">post_context</span><span class="p">,</span>
            <span class="p">)</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_lines_from_file</span><span class="p">(</span>
                <span class="n">filename</span><span class="p">,</span>
                <span class="n">lineno</span><span class="p">,</span>
                <span class="mi">7</span><span class="p">,</span>
                <span class="n">loader</span><span class="p">,</span>
                <span class="n">module_name</span><span class="p">,</span>
            <span class="p">)</span>
            <span class="k">if</span> <span class="n">pre_context_lineno</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">pre_context_lineno</span> <span class="o">=</span> <span class="n">lineno</span>
                <span class="n">pre_context</span> <span class="o">=</span> <span class="p">[]</span>
                <span class="n">context_line</span> <span class="o">=</span> <span class="s2">&quot;&lt;source code not available&gt;&quot;</span>
                <span class="n">post_context</span> <span class="o">=</span> <span class="p">[]</span>

            <span class="n">colno</span> <span class="o">=</span> <span class="n">tb_area_colno</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span>
            <span class="k">if</span> <span class="n">PY311</span><span class="p">:</span>
                <span class="n">_</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">start_column</span><span class="p">,</span> <span class="n">end_column</span> <span class="o">=</span> <span class="nb">next</span><span class="p">(</span>
                    <span class="n">itertools</span><span class="o">.</span><span class="n">islice</span><span class="p">(</span>
                        <span class="n">tb</span><span class="o">.</span><span class="n">tb_frame</span><span class="o">.</span><span class="n">f_code</span><span class="o">.</span><span class="n">co_positions</span><span class="p">(),</span> <span class="n">tb</span><span class="o">.</span><span class="n">tb_lasti</span> <span class="o">//</span> <span class="mi">2</span><span class="p">,</span> <span class="kc">None</span>
                    <span class="p">)</span>
                <span class="p">)</span>
                <span class="k">if</span> <span class="n">start_column</span> <span class="ow">and</span> <span class="n">end_column</span><span class="p">:</span>
                    <span class="n">underline</span> <span class="o">=</span> <span class="s2">&quot;^&quot;</span> <span class="o">*</span> <span class="p">(</span><span class="n">end_column</span> <span class="o">-</span> <span class="n">start_column</span><span class="p">)</span>
                    <span class="n">spaces</span> <span class="o">=</span> <span class="s2">&quot; &quot;</span> <span class="o">*</span> <span class="p">(</span><span class="n">start_column</span> <span class="o">+</span> <span class="nb">len</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">lineno</span> <span class="o">+</span> <span class="mi">1</span><span class="p">))</span> <span class="o">+</span> <span class="mi">2</span><span class="p">)</span>
                    <span class="n">colno</span> <span class="o">=</span> <span class="sa">f</span><span class="s2">&quot;</span><span class="se">\n</span><span class="si">{</span><span class="n">spaces</span><span class="si">}{</span><span class="n">underline</span><span class="si">}</span><span class="s2">&quot;</span>
                    <span class="n">tb_area_spaces</span> <span class="o">=</span> <span class="s2">&quot; &quot;</span> <span class="o">*</span> <span class="p">(</span>
                        <span class="mi">4</span>
                        <span class="o">+</span> <span class="n">start_column</span>
                        <span class="o">-</span> <span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">context_line</span><span class="p">)</span> <span class="o">-</span> <span class="nb">len</span><span class="p">(</span><span class="n">context_line</span><span class="o">.</span><span class="n">lstrip</span><span class="p">()))</span>
                    <span class="p">)</span>
                    <span class="n">tb_area_colno</span> <span class="o">=</span> <span class="sa">f</span><span class="s2">&quot;</span><span class="se">\n</span><span class="si">{</span><span class="n">tb_area_spaces</span><span class="si">}{</span><span class="n">underline</span><span class="si">}</span><span class="s2">&quot;</span>
            <span class="k">yield</span> <span class="p">{</span>
                <span class="s2">&quot;exc_cause&quot;</span><span class="p">:</span> <span class="n">exc_cause</span><span class="p">,</span>
                <span class="s2">&quot;exc_cause_explicit&quot;</span><span class="p">:</span> <span class="n">exc_cause_explicit</span><span class="p">,</span>
                <span class="s2">&quot;tb&quot;</span><span class="p">:</span> <span class="n">tb</span><span class="p">,</span>
                <span class="s2">&quot;type&quot;</span><span class="p">:</span> <span class="s2">&quot;django&quot;</span> <span class="k">if</span> <span class="n">module_name</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s2">&quot;django.&quot;</span><span class="p">)</span> <span class="k">else</span> <span class="s2">&quot;user&quot;</span><span class="p">,</span>
                <span class="s2">&quot;filename&quot;</span><span class="p">:</span> <span class="n">filename</span><span class="p">,</span>
                <span class="s2">&quot;function&quot;</span><span class="p">:</span> <span class="n">function</span><span class="p">,</span>
                <span class="s2">&quot;lineno&quot;</span><span class="p">:</span> <span class="n">lineno</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span>
                <span class="s2">&quot;vars&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">filter</span><span class="o">.</span><span class="n">get_traceback_frame_variables</span><span class="p">(</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">request</span><span class="p">,</span> <span class="n">tb</span><span class="o">.</span><span class="n">tb_frame</span>
                <span class="p">),</span>
                <span class="s2">&quot;id&quot;</span><span class="p">:</span> <span class="nb">id</span><span class="p">(</span><span class="n">tb</span><span class="p">),</span>
                <span class="s2">&quot;pre_context&quot;</span><span class="p">:</span> <span class="n">pre_context</span><span class="p">,</span>
                <span class="s2">&quot;context_line&quot;</span><span class="p">:</span> <span class="n">context_line</span><span class="p">,</span>
                <span class="s2">&quot;post_context&quot;</span><span class="p">:</span> <span class="n">post_context</span><span class="p">,</span>
                <span class="s2">&quot;pre_context_lineno&quot;</span><span class="p">:</span> <span class="n">pre_context_lineno</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span>
                <span class="s2">&quot;colno&quot;</span><span class="p">:</span> <span class="n">colno</span><span class="p">,</span>
                <span class="s2">&quot;tb_area_colno&quot;</span><span class="p">:</span> <span class="n">tb_area_colno</span><span class="p">,</span>
            <span class="p">}</span>
            <span class="n">tb</span> <span class="o">=</span> <span class="n">tb</span><span class="o">.</span><span class="n">tb_next</span></div>



<span class="k">def</span><span class="w"> </span><span class="nf">technical_404_response</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="w">    </span><span class="sd">&quot;&quot;&quot;Create a technical 404 error response. `exception` is the Http404.&quot;&quot;&quot;</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">error_url</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="s2">&quot;path&quot;</span><span class="p">]</span>
    <span class="k">except</span> <span class="p">(</span><span class="ne">IndexError</span><span class="p">,</span> <span class="ne">TypeError</span><span class="p">,</span> <span class="ne">KeyError</span><span class="p">):</span>
        <span class="n">error_url</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">path_info</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>  <span class="c1"># Trim leading slash</span>

    <span class="k">try</span><span class="p">:</span>
        <span class="n">tried</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="s2">&quot;tried&quot;</span><span class="p">]</span>
    <span class="k">except</span> <span class="p">(</span><span class="ne">IndexError</span><span class="p">,</span> <span class="ne">TypeError</span><span class="p">,</span> <span class="ne">KeyError</span><span class="p">):</span>
        <span class="n">resolved</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="n">tried</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">resolver_match</span><span class="o">.</span><span class="n">tried</span> <span class="k">if</span> <span class="n">request</span><span class="o">.</span><span class="n">resolver_match</span> <span class="k">else</span> <span class="kc">None</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">resolved</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">tried</span> <span class="ow">or</span> <span class="p">(</span>  <span class="c1"># empty URLconf</span>
            <span class="n">request</span><span class="o">.</span><span class="n">path_info</span> <span class="o">==</span> <span class="s2">&quot;/&quot;</span>
            <span class="ow">and</span> <span class="nb">len</span><span class="p">(</span><span class="n">tried</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span>
            <span class="ow">and</span> <span class="nb">len</span><span class="p">(</span><span class="n">tried</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="o">==</span> <span class="mi">1</span>  <span class="c1"># default URLconf</span>
            <span class="ow">and</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">tried</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="mi">0</span><span class="p">],</span> <span class="s2">&quot;app_name&quot;</span><span class="p">,</span> <span class="s2">&quot;&quot;</span><span class="p">)</span>
            <span class="o">==</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">tried</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="mi">0</span><span class="p">],</span> <span class="s2">&quot;namespace&quot;</span><span class="p">,</span> <span class="s2">&quot;&quot;</span><span class="p">)</span>
            <span class="o">==</span> <span class="s2">&quot;admin&quot;</span>
        <span class="p">):</span>
            <span class="k">return</span> <span class="n">default_urlconf</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>

    <span class="n">urlconf</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="s2">&quot;urlconf&quot;</span><span class="p">,</span> <span class="n">settings</span><span class="o">.</span><span class="n">ROOT_URLCONF</span><span class="p">)</span>
    <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">urlconf</span><span class="p">,</span> <span class="n">types</span><span class="o">.</span><span class="n">ModuleType</span><span class="p">):</span>
        <span class="n">urlconf</span> <span class="o">=</span> <span class="n">urlconf</span><span class="o">.</span><span class="vm">__name__</span>

    <span class="k">with</span> <span class="n">builtin_template_path</span><span class="p">(</span><span class="s2">&quot;technical_404.html&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">encoding</span><span class="o">=</span><span class="s2">&quot;utf-8&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">fh</span><span class="p">:</span>
        <span class="n">t</span> <span class="o">=</span> <span class="n">DEBUG_ENGINE</span><span class="o">.</span><span class="n">from_string</span><span class="p">(</span><span class="n">fh</span><span class="o">.</span><span class="n">read</span><span class="p">())</span>
    <span class="n">reporter_filter</span> <span class="o">=</span> <span class="n">get_default_exception_reporter_filter</span><span class="p">()</span>
    <span class="n">c</span> <span class="o">=</span> <span class="n">Context</span><span class="p">(</span>
        <span class="p">{</span>
            <span class="s2">&quot;urlconf&quot;</span><span class="p">:</span> <span class="n">urlconf</span><span class="p">,</span>
            <span class="s2">&quot;root_urlconf&quot;</span><span class="p">:</span> <span class="n">settings</span><span class="o">.</span><span class="n">ROOT_URLCONF</span><span class="p">,</span>
            <span class="s2">&quot;request_path&quot;</span><span class="p">:</span> <span class="n">error_url</span><span class="p">,</span>
            <span class="s2">&quot;urlpatterns&quot;</span><span class="p">:</span> <span class="n">tried</span><span class="p">,</span>
            <span class="s2">&quot;resolved&quot;</span><span class="p">:</span> <span class="n">resolved</span><span class="p">,</span>
            <span class="s2">&quot;reason&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="s2">&quot;request&quot;</span><span class="p">:</span> <span class="n">request</span><span class="p">,</span>
            <span class="s2">&quot;settings&quot;</span><span class="p">:</span> <span class="n">reporter_filter</span><span class="o">.</span><span class="n">get_safe_settings</span><span class="p">(),</span>
            <span class="s2">&quot;raising_view_name&quot;</span><span class="p">:</span> <span class="n">get_caller</span><span class="p">(</span><span class="n">request</span><span class="p">),</span>
        <span class="p">}</span>
    <span class="p">)</span>
    <span class="k">return</span> <span class="n">HttpResponseNotFound</span><span class="p">(</span><span class="n">t</span><span class="o">.</span><span class="n">render</span><span class="p">(</span><span class="n">c</span><span class="p">))</span>


<span class="k">def</span><span class="w"> </span><span class="nf">default_urlconf</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;Create an empty URLconf 404 error response.&quot;&quot;&quot;</span>
    <span class="k">with</span> <span class="n">builtin_template_path</span><span class="p">(</span><span class="s2">&quot;default_urlconf.html&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">encoding</span><span class="o">=</span><span class="s2">&quot;utf-8&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">fh</span><span class="p">:</span>
        <span class="n">t</span> <span class="o">=</span> <span class="n">DEBUG_ENGINE</span><span class="o">.</span><span class="n">from_string</span><span class="p">(</span><span class="n">fh</span><span class="o">.</span><span class="n">read</span><span class="p">())</span>
    <span class="n">c</span> <span class="o">=</span> <span class="n">Context</span><span class="p">(</span>
        <span class="p">{</span>
            <span class="s2">&quot;version&quot;</span><span class="p">:</span> <span class="n">get_docs_version</span><span class="p">(),</span>
        <span class="p">}</span>
    <span class="p">)</span>

    <span class="k">return</span> <span class="n">HttpResponse</span><span class="p">(</span><span class="n">t</span><span class="o">.</span><span class="n">render</span><span class="p">(</span><span class="n">c</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>