From 2eac9899856d0e19c8e2df02256f2e8c4bdb5344 Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Sun, 4 Aug 2013 11:01:01 +0200 Subject: [PATCH] [1.6.x] Fixed #20822 -- Set content type of default error pages to 'text/html'. Thanks Jimmy Song for the patch. Backport of 7843775 from master. --- django/views/defaults.py | 11 +++++++---- tests/view_tests/tests/test_defaults.py | 20 +++++++++++++++++++- tests/view_tests/urls.py | 4 +++- tests/view_tests/views.py | 11 +++++++---- 4 files changed, 36 insertions(+), 10 deletions(-) diff --git a/django/views/defaults.py b/django/views/defaults.py index c8a62fc7532..d531f2237b6 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 5efd338d345..df891add7ea 100644 --- a/tests/view_tests/tests/test_defaults.py +++ b/tests/view_tests/tests/test_defaults.py @@ -2,7 +2,8 @@ from __future__ import absolute_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 d792e47ddfb..b0c06a1e560 100644 --- a/tests/view_tests/urls.py +++ b/tests/view_tests/urls.py @@ -47,8 +47,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 1cfafa4333d..5fcf1703d49 100644 --- a/tests/view_tests/views.py +++ b/tests/view_tests/views.py @@ -2,7 +2,7 @@ from __future__ import absolute_import 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.