Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[1.6.x] Fixed #20822 -- Set content type of default error pages to 't…

…ext/html'.

Thanks Jimmy Song for the patch.

Backport of 7843775 from master.
  • Loading branch information...
commit 2eac9899856d0e19c8e2df02256f2e8c4bdb5344 1 parent ccef8b2
@aaugustin aaugustin authored
View
11 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(
'<h1>Not Found</h1>'
'<p>The requested URL {{ request_path }} was not found on this server.</p>')
- 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('<h1>Server Error (500)</h1>')
+ return http.HttpResponseServerError('<h1>Server Error (500)</h1>', 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('<h1>Bad Request (400)</h1>')
+ return http.HttpResponseBadRequest('<h1>Bad Request (400)</h1>', 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('<h1>403 Forbidden</h1>')
+ return http.HttpResponseForbidden('<h1>403 Forbidden</h1>', content_type='text/html')
return http.HttpResponseForbidden(template.render(RequestContext(request)))
View
20 tests/view_tests/tests/test_defaults.py
@@ -2,7 +2,8 @@
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 @@ def test_get_absolute_url_attributes(self):
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')
View
4 tests/view_tests/urls.py
@@ -47,8 +47,10 @@
# 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')),
View
11 tests/view_tests/views.py
@@ -2,7 +2,7 @@
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 callable():
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.
Please sign in to comment.
Something went wrong with that request. Please try again.