Django 1.5 using a cached HttpResponse with WSGI has an empty body
|Reported by:||Owned by:||Aymeric Augustin|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
With the change to HttpResponse made in Django 1.5, I'm finding that in my code, which caches a generated response, results in an empty body when that page is requested a second time. The first time the page is requested, it is not in the cache, and the page is generated normally and added to the cache. A subsequent request for the same page finds the response in the cache and that response is returned, but with a content length of zero.
The reason is that the HttpResponse in Django 1.5 does not reset the content iterator when the content is requested to be iterated over again (the next time the response content is required).
I note the comments made about the way an iterator should behave when requested to iterate again:
and the code which was added to explicitly prevent a reiteration from resetting the iterator. However, that causes a problem when using cached responses.
The HttpResponse in my case was not created by passing an iterator to HttpResponse. It is just using a string.
The problem is that the
__iter__ method of HttpResponse contains the line:
if not hasattr(self, '_iterator'): self._iterator = iter(self._container)
This prevents the iterator being reset the next time it is required to iterate over the content.
_container still has the original content, but
__iter__ does not reset the iterator as _iterator exists as an attribute since the first time that response was returned. The cached response is returning a used iterator, which returns no content.
I suspect this is a bug.
What about a work-around in the meantime?
When I retrieve the response from the cache, I could do:
response._iterator = iter(response._container)
This works, but makes my code dependent on the internals of the HttpResponse class, which isn't great.
My suggestion is to change the
__iter__ method for HttpResponse such that it creates an instance of a separate iterator class. The
__iter__ method should not return self. It would be OK for the
__iter__ method of the separate iterator class to return self. This should avoid the problem reported in https://code.djangoproject.com/ticket/13222.
I accept that StreamingHttpResponse instances can only be iterated once, and are therefore not suitable for caching.
Change History (11)
comment:4 Changed 3 years ago by
|Owner:||changed from nobody to Aymeric Augustin|
|Status:||new → assigned|