Create a technical 404 error response. `exception` is the Http404.
(request, exception)
| 612 | @csp_override({}) |
| 613 | @csp_report_only_override({}) |
| 614 | def technical_404_response(request, exception): |
| 615 | """Create a technical 404 error response. `exception` is the Http404.""" |
| 616 | try: |
| 617 | error_url = exception.args[0]["path"] |
| 618 | except (IndexError, TypeError, KeyError): |
| 619 | error_url = request.path_info[1:] # Trim leading slash |
| 620 | |
| 621 | try: |
| 622 | tried = exception.args[0]["tried"] |
| 623 | except (IndexError, TypeError, KeyError): |
| 624 | resolved = True |
| 625 | tried = request.resolver_match.tried if request.resolver_match else None |
| 626 | else: |
| 627 | resolved = False |
| 628 | if not tried or ( # empty URLconf |
| 629 | request.path_info == "/" |
| 630 | and len(tried) == 1 |
| 631 | and len(tried[0]) == 1 # default URLconf |
| 632 | and getattr(tried[0][0], "app_name", "") |
| 633 | == getattr(tried[0][0], "namespace", "") |
| 634 | == "admin" |
| 635 | ): |
| 636 | return default_urlconf(request) |
| 637 | |
| 638 | patterns_with_debug_info = [] |
| 639 | for urlpattern in tried or (): |
| 640 | patterns = [] |
| 641 | for inner_pattern in urlpattern: |
| 642 | wrapper = {"tried": inner_pattern} |
| 643 | if isinstance(inner_pattern, URLResolver): |
| 644 | wrapper["debug_key"] = "namespace" |
| 645 | wrapper["debug_val"] = inner_pattern.namespace |
| 646 | else: |
| 647 | wrapper["debug_key"] = "name" |
| 648 | wrapper["debug_val"] = inner_pattern.name |
| 649 | patterns.append(wrapper) |
| 650 | patterns_with_debug_info.append(patterns) |
| 651 | |
| 652 | urlconf = getattr(request, "urlconf", settings.ROOT_URLCONF) |
| 653 | if isinstance(urlconf, types.ModuleType): |
| 654 | urlconf = urlconf.__name__ |
| 655 | |
| 656 | with builtin_template_path("technical_404.html").open(encoding="utf-8") as fh: |
| 657 | t = DEBUG_ENGINE.from_string(fh.read()) |
| 658 | reporter_filter = get_default_exception_reporter_filter() |
| 659 | c = Context( |
| 660 | { |
| 661 | "urlconf": urlconf, |
| 662 | "root_urlconf": settings.ROOT_URLCONF, |
| 663 | "request_path": error_url, |
| 664 | "urlpatterns": tried, # Unused, left for compatibility. |
| 665 | "urlpatterns_debug": patterns_with_debug_info, |
| 666 | "resolved": resolved, |
| 667 | "reason": str(exception), |
| 668 | "request": request, |
| 669 | "settings": reporter_filter.get_safe_settings(), |
| 670 | "raising_view_name": get_caller(request), |
| 671 | } |