Skip to content

1347: improving 404 page #1919

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 40 additions & 12 deletions djangoproject/templates/404.html
Original file line number Diff line number Diff line change
@@ -1,23 +1,51 @@
{% extends 'base_error.html' %}
{% load i18n %}
{% load custom_tags %}

{% block title %}{% translate "Page not found" %}{% endblock %}

{% block header %}<h1>404</h1>{% endblock %}
{% block header %}
<h1 class="error-title">404 - Page Not Found</h1>
<p class="error-subtitle">
{% translate "Oops! The page you’re looking for doesn’t exist." %}
</p>
{% endblock %}

{% block content %}
<h2>{% translate "Page not found" %}</h2>
<div class="error-container">
<h2>{% translate "We couldn’t find the page:" %}</h2>
<p class="broken-url">"<strong>{{ broken_path }}</strong>"</p>

<p>
{% blocktranslate trimmed %}
Looks like you followed a bad link. If you think it's our fault, please
<a href="https://code.djangoproject.com/">let us know</a>.
{% endblocktranslate %}
</p>

<p>
{% blocktranslate trimmed %}
Looks like you followed a bad link. If you think it's our fault, please
<a href="https://code.djangoproject.com/">let us know</a>.
{% endblocktranslate %}</p>
<!-- Search Form -->
<div class="search-container">
<form method="get" action="{% url 'doc_search' %}">
{{ form.as_p }}
<button type="submit" class="search-button">{% translate "Search Docs" %}</button>
</form>
</div>

{% url 'homepage' as homepage_url %}
<p>
{% blocktranslate trimmed %}
Here's a link to the <a href="{{ homepage_url }}">homepage</a>. You know, just in case.
{% endblocktranslate %}</p>
<!-- Suggested Closest Matches -->
{% if suggestions %}
<h3>{% translate "Did you mean?" %}</h3>
<ul class="suggestion-list">
{% for suggestion in suggestions %}
<li><a href="{{ suggestion }}">{{ suggestion }}</a></li>
{% endfor %}
</ul>
{% endif %}

{% url 'homepage' as homepage_url %}
<p>
{% blocktranslate trimmed %}
Here's a link to the <a href="{{ homepage_url }}">homepage</a>. You know, just in case.
{% endblocktranslate %}
</p>
</div>
{% endblock %}
38 changes: 32 additions & 6 deletions docs/templatetags/docs.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import difflib

from django import template
from django.conf import settings
from django.http import Http404
from django.shortcuts import get_object_or_404
from django.urls import reverse
from django.utils.safestring import mark_safe
from django.utils.version import get_version_tuple
from pygments import highlight
Expand All @@ -11,18 +17,38 @@

register = template.Library()

# List of existing docs URLs for similarity matching
EXISTING_DOCS_URLS = [
"/en/4.2/ref/databases/",
"/en/4.2/ref/models/",
"/en/4.2/topics/db/",
"/en/4.2/howto/custom-model-fields/",
"/en/4.2/ref/settings/",
# Add more known URLs as needed
]


def get_suggestions(broken_url):
"""Finds close matches for the broken URL using difflib."""
return difflib.get_close_matches(broken_url, EXISTING_DOCS_URLS, n=3, cutoff=0.6)


@register.inclusion_tag("docs/search_form.html", takes_context=True)
def search_form(context):
request = context["request"]
release = DocumentRelease.objects.get_by_version_and_lang(
context["version"],
context["lang"],
)
version = context.get("version", context.get("DJANGO_VERSION"))
lang = context.get("lang", context.get("LANGUAGE_CODE"))

release = get_object_or_404(DocumentRelease, version=version, lang=lang)
broken_path = request.path.replace(f"/{lang}/{version}/", "", 1)
suggestions = get_suggestions(f"/{broken_path}")

return {
"form": DocSearchForm(request.GET, release=release),
"version": context["version"],
"lang": context["lang"],
"version": version,
"lang": lang,
"suggestions": suggestions,
"broken_path": broken_path,
}


Expand Down