Repository: django/django
File: django/views/debug.py (Python)

Code before:
    try:
        tried = exception.args[0]["tried"]
    except (IndexError, TypeError, KeyError):
        resolved = True
        tried = request.resolver_match.tried if request.resolver_match else None
    else:
        resolved = False
        if not tried or (  # empty URLconf
            request.path_info == "/"
            and len(tried) == 1
            and len(tried[0]) == 1  # default URLconf
            and getattr(tried[0][0], "app_name", "")
            == getattr(tried[0][0], "namespace", "")
            == "admin"
        ):
            return default_urlconf(request)

    for inner_tried in itertools.chain(*tried or []):
        if isinstance(inner_tried, URLResolver):
            inner_tried.debug_key_val = TriedPatternDebugInfo(
                key="namespace", val=inner_tried.namespace
            )
        else:
            inner_tried.debug_key_val = TriedPatternDebugInfo(
                key="name", val=inner_tried.name
            )

    urlconf = getattr(request, "urlconf", settings.ROOT_URLCONF)
    if isinstance(urlconf, types.ModuleType):
        urlconf = urlconf.__name__

    with builtin_template_path("technical_404.html").open(encoding="utf-8") as fh:
        t = DEBUG_ENGINE.from_string(fh.read())
    reporter_filter = get_default_exception_reporter_filter()
    c = Context(
        {
            "urlconf": urlconf,
            "root_urlconf": settings.ROOT_URLCONF,
            "request_path": error_url,
            "urlpatterns": tried,
            "resolved": resolved,
            "reason": str(exception),
            "request": request,
            "settings": reporter_filter.get_safe_settings(),
            "raising_view_name": get_caller(request),
        }
    )
    return HttpResponseNotFound(t.render(c))


def default_urlconf(request):

Review comment (bug):
```suggestion
        if isinstance(inner_tried, URLResolver):
            inner_tried.debug_key_val = {
                "key": "namespace",
                "val": inner_tried.namespace,
            }
        else:
            inner_tried.debug_key_val = {"key": "name", "val": inner_tried.name}
```

I wouldn't add a `namedtuple` unless we had to

Code after:
        tried = exception.args[0]["tried"]
    except (IndexError, TypeError, KeyError):
        resolved = True
        tried = request.resolver_match.tried if request.resolver_match else None
    else:
        resolved = False
        if not tried or (  # empty URLconf
            request.path_info == "/"
            and len(tried) == 1
            and len(tried[0]) == 1  # default URLconf
            and getattr(tried[0][0], "app_name", "")
            == getattr(tried[0][0], "namespace", "")
            == "admin"
        ):
            return default_urlconf(request)

    patterns_with_debug_info = []
    for urlpattern in tried or ():
        patterns = []
        for inner_pattern in urlpattern:
            wrapper = {"tried": inner_pattern}
            if isinstance(inner_pattern, URLResolver):
                wrapper["debug_key"] = "namespace"
                wrapper["debug_val"] = inner_pattern.namespace
            else:
                wrapper["debug_key"] = "name"
                wrapper["debug_val"] = inner_pattern.name
            patterns.append(wrapper)
        patterns_with_debug_info.append(patterns)

    urlconf = getattr(request, "urlconf", settings.ROOT_URLCONF)
    if isinstance(urlconf, types.ModuleType):
        urlconf = urlconf.__name__

    with builtin_template_path("technical_404.html").open(encoding="utf-8") as fh:
        t = DEBUG_ENGINE.from_string(fh.read())
    reporter_filter = get_default_exception_reporter_filter()
    c = Context(
        {
            "urlconf": urlconf,
            "root_urlconf": settings.ROOT_URLCONF,
            "request_path": error_url,
            "urlpatterns": tried,  # Unused, left for compatibility.
            "urlpatterns_debug": patterns_with_debug_info,
            "resolved": resolved,
            "reason": str(exception),
            "request": request,
            "settings": reporter_filter.get_safe_settings(),
            "raising_view_name": get_caller(request),
        }
    )