[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.
This commit is contained in:
parent
ccef8b2aa2
commit
2eac989985
|
@ -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)))
|
||||
|
||||
|
||||
|
|
|
@ -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')
|
||||
|
|
|
@ -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')),
|
||||
|
|
|
@ -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.
|
||||
|
|
Loading…
Reference in New Issue