def my_render_callback(response):
return response from django.template.response import TemplateResponse
def my_templateresponse(request):
response = TemplateResponse(request, 'template.html', {})
response.add_post_render_callback(my_render_callback);
return response
we call add_post_render_callback to register a callback function.
in base.py , get_response
if hasattr(response, 'render') and callable(response.render):
for middleware_method in self._template_response_middleware:
response = middleware_method(request, response)
response = response.render()
The response is a TemplateResponse, the subclass of SimpleTemplateResponse
it has a 'render' function
def render(self):
"""Renders (thereby finalizing) the content of the response. If the content has already been rendered, this is a no-op. Returns the baked response instance.
"""
retval = self
if not self._is_rendered:
self.content = self.rendered_content
for post_callback in self._post_render_callbacks:
newretval = post_callback(retval)
if newretval is not None:
retval = newretval
return retval
需要注意的是这个callback的返回值, 如果 not None, 它的返回值就是我们最后的response,