Opened 14 years ago

Closed 14 years ago

Last modified 14 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: dev
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: no

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 by Aymeric Augustin, 14 years ago

Easy pickings: 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 by Waldemar Kornewald, 14 years ago

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

comment:3 by Aymeric Augustin, 14 years ago

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