Refs #31224 -- Added BaseHandler.check_response().

This commit is contained in:
Andrew Godwin 2020-02-19 13:20:16 +01:00 committed by Mariusz Felisiak
parent b457068cf2
commit 1e858adf7f
1 changed files with 25 additions and 17 deletions

View File

@ -115,30 +115,21 @@ class BaseHandler:
response = self.process_exception_by_middleware(e, request) response = self.process_exception_by_middleware(e, request)
# Complain if the view returned None (a common error). # Complain if the view returned None (a common error).
if response is None: self.check_response(response, callback)
if isinstance(callback, types.FunctionType): # FBV
view_name = callback.__name__
else: # CBV
view_name = callback.__class__.__name__ + '.__call__'
raise ValueError(
"The view %s.%s didn't return an HttpResponse object. It "
"returned None instead." % (callback.__module__, view_name)
)
# If the response supports deferred rendering, apply template # If the response supports deferred rendering, apply template
# response middleware and then render the response # response middleware and then render the response
elif hasattr(response, 'render') and callable(response.render): if hasattr(response, 'render') and callable(response.render):
for middleware_method in self._template_response_middleware: for middleware_method in self._template_response_middleware:
response = middleware_method(request, response) response = middleware_method(request, response)
# Complain if the template response middleware returned None (a common error). # Complain if the template response middleware returned None (a common error).
if response is None: self.check_response(
raise ValueError( response,
"%s.process_template_response didn't return an " middleware_method,
"HttpResponse object. It returned None instead." name='%s.process_template_response' % (
% (middleware_method.__self__.__class__.__name__) middleware_method.__self__.__class__.__name__,
) )
)
try: try:
response = response.render() response = response.render()
except Exception as e: except Exception as e:
@ -157,6 +148,23 @@ class BaseHandler:
return response return response
raise raise
def check_response(self, response, callback, name=None):
"""Raise an error if the view returned None."""
if response is not None:
return
if not name:
if isinstance(callback, types.FunctionType): # FBV
name = 'The view %s.%s' % (callback.__module__, callback.__name__)
else: # CBV
name = 'The view %s.%s.__call__' % (
callback.__module__,
callback.__class__.__name__,
)
raise ValueError(
"%s didn't return an HttpResponse object. It returned None "
"instead." % name
)
def reset_urlconf(sender, **kwargs): def reset_urlconf(sender, **kwargs):
"""Reset the URLconf after each request is finished.""" """Reset the URLconf after each request is finished."""