mirror of
https://github.com/django/django.git
synced 2025-01-08 17:37:20 +00:00
Refs #24829 -- Made TemplateResponse.content available sooner in exception context
Thanks Tim Graham for the initial patch.
This commit is contained in:
parent
9f66302797
commit
742ea51413
@ -85,6 +85,10 @@ def response_for_exception(request, exc):
|
|||||||
signals.got_request_exception.send(sender=None, request=request)
|
signals.got_request_exception.send(sender=None, request=request)
|
||||||
response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
|
response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
|
||||||
|
|
||||||
|
# Force a TemplateResponse to be rendered.
|
||||||
|
if not getattr(response, 'is_rendered', True) and callable(getattr(response, 'render', None)):
|
||||||
|
response = response.render()
|
||||||
|
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,7 +1,19 @@
|
|||||||
from django.conf.urls import url
|
from django.conf.urls import url
|
||||||
from django.core.exceptions import PermissionDenied
|
from django.core.exceptions import PermissionDenied
|
||||||
from django.template.response import TemplateResponse
|
from django.template.response import TemplateResponse
|
||||||
from django.test import SimpleTestCase, override_settings
|
from django.test import SimpleTestCase, modify_settings, override_settings
|
||||||
|
|
||||||
|
|
||||||
|
class MiddlewareAccessingContent(object):
|
||||||
|
def __init__(self, get_response):
|
||||||
|
self.get_response = get_response
|
||||||
|
|
||||||
|
def __call__(self, request):
|
||||||
|
response = self.get_response(request)
|
||||||
|
# Response.content should be available in the middleware even with a
|
||||||
|
# TemplateResponse-based exception response.
|
||||||
|
assert response.content
|
||||||
|
return response
|
||||||
|
|
||||||
|
|
||||||
def template_response_error_handler(request, exception=None):
|
def template_response_error_handler(request, exception=None):
|
||||||
@ -20,6 +32,7 @@ handler403 = template_response_error_handler
|
|||||||
|
|
||||||
|
|
||||||
@override_settings(ROOT_URLCONF='handlers.tests_custom_error_handlers')
|
@override_settings(ROOT_URLCONF='handlers.tests_custom_error_handlers')
|
||||||
|
@modify_settings(MIDDLEWARE={'append': 'handlers.tests_custom_error_handlers.MiddlewareAccessingContent'})
|
||||||
class CustomErrorHandlerTests(SimpleTestCase):
|
class CustomErrorHandlerTests(SimpleTestCase):
|
||||||
|
|
||||||
def test_handler_renders_template_response(self):
|
def test_handler_renders_template_response(self):
|
||||||
|
Loading…
Reference in New Issue
Block a user