Skip to content

Show some useful links on 404 default page #5091

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

Closed
wants to merge 1 commit into from
Closed
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
24 changes: 23 additions & 1 deletion readthedocs/core/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,33 @@ def server_error_404(request, exception=None, template_name='404.html'): # pyli
)
else:
return response
r = render(request, template_name)
r = render(request, template_name, context=_get_404_context(request))
r.status_code = 404
return r


def _get_404_context(request):
from readthedocs.redirects.utils import project_and_path_from_request, language_and_version_from_path
project = None
language = None
version_slug = None

full_path = request.get_full_path()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this suffer from the Django security issue they just patched? django/django@1cd00fc

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, but we aren't calling urlquote, which is the fix for the path issue.

project, path = project_and_path_from_request(request, full_path)
if project and not project.single_version:
language, version_slug, path = language_and_version_from_path(path)

context = {
'default_docs_url': project.get_docs_url(),
'version_docs_url': project.get_docs_url(
version_slug=version_slug,
lang_slug=language,
),
'full_path': full_path,
}
return context


def do_not_track(request):
dnt_header = request.META.get('HTTP_DNT')

Expand Down
5 changes: 5 additions & 0 deletions readthedocs/core/views/serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ def serve_docs(
request, project, subproject, lang_slug=None, version_slug=None,
filename=''):
"""Exists to map existing proj, lang, version, filename views to the file format."""

# TODO: remove this, it's only for debugging purposes
# from readthedocs.core.views import server_error_404
# return server_error_404(request)

if not version_slug:
version_slug = project.get_default_version()
try:
Expand Down
33 changes: 14 additions & 19 deletions readthedocs/templates/404.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,20 @@
{% block language-select-form %}{% endblock %}

{% block content %}
{% if suggestion %}
<div class="suggestions">
<h1>You've found something that doesn't exist.</h1>
<p>{{ suggestion.message }}</p>
{% ifequal suggestion.type 'top' %}
<p>
<a href="{{ suggestion.href }}">Go to the top of the documentation.</a>
</p>
{% endifequal %}
{% ifequal suggestion.type 'list' %}
<ul>
{% for item in suggestion.list %}
<li><a href="{% doc_url item.project item.version_slug item.pagename %}">{{ item.label }}</a></li>
{% endfor %}
</ul>
{% endifequal %}
</div>

{% block 404_useful_links %}
{% if default_docs_url %}
Top site default documentation: {{ default_docs_url }} </br>
{% endif %}
{% if version_docs_url %}
Version/Language site documentation: {{ version_docs_url }} </br>
{% endif %}
<pre style="line-height: 1.25; white-space: pre;">
{% if full_path %}
The page requested is {{ full_path }}</br>
{% endif %}
{% endblock %}

<pre style="%; line-height: 1.25; white-space: pre;">

\ SORRY /
\ /
Expand All @@ -58,5 +53,5 @@ <h1>You've found something that doesn't exist.</h1>
/ N \
/ q, \
/ \
</pre>
</pre>
{% endblock %}