diff --git a/django/views/defaults.py b/django/views/defaults.py index e9d0f16a33..2f54fcd263 100644 --- a/django/views/defaults.py +++ b/django/views/defaults.py @@ -21,11 +21,14 @@ def page_not_found(request, template_name='404.html'): """ try: template = loader.get_template(template_name) + content_type = None # Django will use DEFAULT_CONTENT_TYPE except TemplateDoesNotExist: template = Template( '

Not Found

' '

The requested URL {{ request_path }} was not found on this server.

') - return http.HttpResponseNotFound(template.render(RequestContext(request, {'request_path': request.path}))) + content_type = 'text/html' + body = template.render(RequestContext(request, {'request_path': request.path})) + return http.HttpResponseNotFound(body, content_type=content_type) @requires_csrf_token @@ -39,7 +42,7 @@ def server_error(request, template_name='500.html'): try: template = loader.get_template(template_name) except TemplateDoesNotExist: - return http.HttpResponseServerError('

Server Error (500)

') + return http.HttpResponseServerError('

Server Error (500)

', content_type='text/html') return http.HttpResponseServerError(template.render(Context({}))) @@ -54,7 +57,7 @@ def bad_request(request, template_name='400.html'): try: template = loader.get_template(template_name) except TemplateDoesNotExist: - return http.HttpResponseBadRequest('

Bad Request (400)

') + return http.HttpResponseBadRequest('

Bad Request (400)

', content_type='text/html') return http.HttpResponseBadRequest(template.render(Context({}))) @@ -75,7 +78,7 @@ def permission_denied(request, template_name='403.html'): try: template = loader.get_template(template_name) except TemplateDoesNotExist: - return http.HttpResponseForbidden('

403 Forbidden

') + return http.HttpResponseForbidden('

403 Forbidden

', content_type='text/html') return http.HttpResponseForbidden(template.render(RequestContext(request))) diff --git a/tests/view_tests/tests/test_defaults.py b/tests/view_tests/tests/test_defaults.py index af812b4446..d55ed53454 100644 --- a/tests/view_tests/tests/test_defaults.py +++ b/tests/view_tests/tests/test_defaults.py @@ -2,7 +2,8 @@ from __future__ import unicode_literals from django.contrib.contenttypes.models import ContentType from django.test import TestCase -from django.test.utils import setup_test_template_loader, restore_template_loaders +from django.test.utils import (setup_test_template_loader, + restore_template_loaders, override_settings) from ..models import Author, Article, UrlArticle @@ -59,3 +60,20 @@ class DefaultsTests(TestCase): article = UrlArticle.objects.get(pk=1) self.assertTrue(getattr(article.get_absolute_url, 'purge', False), 'The attributes of the original get_absolute_url must be added.') + + @override_settings(DEFAULT_CONTENT_TYPE="text/xml") + def test_default_content_type_is_text_html(self): + """ + Content-Type of the default error responses is text/html. Refs #20822. + """ + response = self.client.get('/views/raises400/') + self.assertEqual(response['Content-Type'], 'text/html') + + response = self.client.get('/views/raises403/') + self.assertEqual(response['Content-Type'], 'text/html') + + response = self.client.get('/views/non_existing_url/') + self.assertEqual(response['Content-Type'], 'text/html') + + response = self.client.get('/views/server_error/') + self.assertEqual(response['Content-Type'], 'text/html') diff --git a/tests/view_tests/urls.py b/tests/view_tests/urls.py index 3dcee2fe53..f2c910368d 100644 --- a/tests/view_tests/urls.py +++ b/tests/view_tests/urls.py @@ -45,8 +45,10 @@ urlpatterns = patterns('', # a view that raises an exception for the debug view (r'raises/$', views.raises), - (r'raises404/$', views.raises404), + + (r'raises400/$', views.raises400), (r'raises403/$', views.raises403), + (r'raises404/$', views.raises404), # i18n views (r'^i18n/', include('django.conf.urls.i18n')), diff --git a/tests/view_tests/views.py b/tests/view_tests/views.py index a40b1e0c57..0bac7d9321 100644 --- a/tests/view_tests/views.py +++ b/tests/view_tests/views.py @@ -2,7 +2,7 @@ from __future__ import unicode_literals import sys -from django.core.exceptions import PermissionDenied +from django.core.exceptions import PermissionDenied, SuspiciousOperation from django.core.urlresolvers import get_resolver from django.http import HttpResponse, HttpResponseRedirect from django.shortcuts import render_to_response, render @@ -31,13 +31,16 @@ def raises(request): except Exception: return technical_500_response(request, *sys.exc_info()) -def raises404(request): - resolver = get_resolver(None) - resolver.resolve('') +def raises400(request): + raise SuspiciousOperation def raises403(request): raise PermissionDenied +def raises404(request): + resolver = get_resolver(None) + resolver.resolve('') + def redirect(request): """ Forces an HTTP redirect.