Skip to content

Serve custom 404 pages from Django #5644

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

Merged
merged 2 commits into from
May 9, 2019
Merged
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
5 changes: 2 additions & 3 deletions readthedocs/core/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@
from django.http import HttpResponseRedirect, Http404, JsonResponse
from django.shortcuts import render, get_object_or_404, redirect
from django.views.generic import TemplateView

from django.views.static import serve as static_serve

from readthedocs.builds.models import Version
from readthedocs.core.utils.general import wipe_version_via_slugs
from readthedocs.core.resolver import resolve_path
from readthedocs.core.symlink import PrivateSymlink, PublicSymlink
from readthedocs.core.views.serve import _serve_file
from readthedocs.projects.constants import PRIVATE
from readthedocs.projects.models import HTMLFile, Project
from readthedocs.redirects.utils import (
Expand Down Expand Up @@ -205,7 +204,7 @@ def resolve_404_path(project, version_slug=None, language=None):
project.slug,
slug,
)
r = _serve_file(request, filename, basepath)
r = static_serve(request, filename, basepath)
r.status_code = 404
return r

Expand Down
8 changes: 2 additions & 6 deletions readthedocs/rtd_tests/tests/test_doc_serving.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,14 +201,11 @@ def test_custom_robots_txt(self, os_mock, open_mock):
ROOT_URLCONF=settings.SUBDOMAIN_URLCONF,
)

@patch('readthedocs.core.views.serve.os')
@patch('readthedocs.core.views.static_serve')
@patch('readthedocs.core.views.os')
def test_custom_404_page(self, os_view_mock, os_serve_mock):
def test_custom_404_page(self, os_view_mock, static_serve_mock):
os_view_mock.path.exists.return_value = True

os_serve_mock.path.join.side_effect = os.path.join
os_serve_mock.path.exists.return_value = True

self.public.versions.update(active=True, built=True)

factory = RequestFactory()
Expand All @@ -221,7 +218,6 @@ def test_custom_404_page(self, os_view_mock, os_serve_mock):
middleware.process_request(request)
response = server_error_404_subdomain(request)
self.assertEqual(response.status_code, 404)
self.assertTrue(response['X-Accel-Redirect'].endswith('/public/en/latest/404.html'))

@override_settings(
USE_SUBDOMAIN=True,
Expand Down