Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#15835 closed Bug (invalid)

middleware.process_response(): TemplateResponse.render() isn't called

Reported by: Waldemar Kornewald Owned by: nobody
Component: Core (Other) Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX:

Description

When you return a TemplateResponse from a middleware's process_response() method the response's render() method isn't called. This causes the following exception: "ContentNotRenderedError: The response content must be rendered before it can be iterated over."

Change History (3)

comment:1 Changed 5 years ago by Aymeric Augustin

Easy pickings: unset
Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset
Resolution: invalid
Status: newclosed

It think this works as designed.

From http://docs.djangoproject.com/en/dev/ref/template-response/:

<quote>

There are three circumstances under which a TemplateResponse will be rendered:

  • When the TemplateResponse instance is explicitly rendered, using the SimpleTemplateResponse.render() method.
  • When the content of the response is explicitly set by assigning response.content.
  • After passing through template response middleware, but before passing through response middleware.

</quote>

If you are using it in the response middleware, you bypass the third item which usually ensures that the rendering happens. You should call render() on the TemplateResponse object before returning it.

comment:2 Changed 5 years ago by Waldemar Kornewald

I'd argue that this is not well designed, then. Why should we have different behavior in response middleware?

comment:3 Changed 5 years ago by Aymeric Augustin

I am not sure I understand your point. "template response" and "response" phases happen at different points in the request-response cycle; middlewares obviously get different possibilities at each step.

Anyway, if you want to discuss the general design of Django's middleware, you should propose a better alternative on django-developers.

Note: See TracTickets for help on using tickets.
Back to Top