In Django, I try to logging the request and response content length, which exactly the same as what Django server prints to stderr.
[05/Apr/2011 22:59:08] "GET /pages/ HTTP/1.1" 200 332161
[05/Apr/2011 22:59:15] "GET /pages/12 HTTP/1.1" 301 0
[05/Apr/2011 22:59:15] "GET /pages/12/ HTTP/1.1" 200 361474
[05/Apr/2011 22:59:16] "GET /pages/12/load/tags/ HTTP/1.1" 200 13899
[05/Apr/2011 22:59:16] "GET /pages/12/load/comments/ HTTP/1.1" 200 82
So, I write a simple middleware as follows, but, the value of 'Content-Length' is always empty.
class LogHttpResponse(object):
def process_response(self, request, response):
import datetime
print response.items()
time_text = datetime.datetime.now().strftime('%m/%d/%Y %H:%M:%S')
print '[%s] "%s %s" %d %s' % (time_text, request.method, request.path,
response.status_code,
response.get('Content-Length', ''))
return response
I've checked through fire-debug, there is 'Content-Length' in the response headers. But there is no 'Content-Length' in the middleware, "print response.items()" shows:
[('Content-Type', 'text/html; charset=utf-8')]
Is there any problem of the middleware orders?
HttpResponse (source code) provides an inbound HTTP request to a Django web application with a text response. This class is most frequently used as a return object from a Django view.
method == "POST" is a boolean value - True if the current request from a user was performed using the HTTP "POST" method, of False otherwise (usually that means HTTP "GET", but there are also other methods).
Django uses request and response objects to pass state through the system. When a page is requested, Django creates an HttpRequest object that contains metadata about the request. Then Django loads the appropriate view, passing the HttpRequest as the first argument to the view function.
Layers of Django Application Django request middlewares follows the order while processing the request. Suppose if we have request middlewares in the order A, B, C then the request first processed by the middleware A and then B and then C. Django comes up with bunch of default middlewares.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With