[Solved]-How do I get the content length of a Django response object?


How about len(response.content)? That’d give you the number of characters in the response’s content. I guess that isn’t necessarily the same as the number of bytes.


I’ve checked through fire-debug, there is ‘Content-Length’ in the
response headers. But there is no ‘Content-Length’ in the middleware
[…] Is there any problem of the middleware orders?

Yes. Middleware classes are applied from top-down (in settings.MIDDLEWARE_CLASSES) when processing request and bottom-up when processing the response. If you have 'django.middleware.http.ConditionalGetMiddleware' in you middleware classes it will add a 'Content-Length' header to the HttpResponse.

Though if you put your middleware class after 'django.middleware.http.ConditionalGetMiddleware' in settings.MIDDLEWARE_CLASSES it will apply this one first when processing the response and then apply the ConditionalMiddleware afterwards. That’s why you see a Content-Length header in Firebug, though its not yet processed when you Middleware is called.

See Django Middleware documentation for more information about Middleware’s.

Leave a comment