[英]How do I get the content length of a Django response object?
In Django, I try to logging the request and response content length, which exactly the same as what Django server prints to stderr. 在Django中,我尝试记录请求和响应内容长度,这与Django服务器打印到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. 所以,我写了一个简单的中间件如下,但是,'Content-Length'的值总是空的。
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 . 我通过fire-debug检查过, 响应头中有'Content-Length' 。 But there is no 'Content-Length' in the middleware, "print response.items()" shows:
但是中间件中没有“内容长度”,“print response.items()”显示:
[('Content-Type', 'text/html; charset=utf-8')]
Is there any problem of the middleware orders? 中间件订单有什么问题吗?
I've checked through fire-debug, there is 'Content-Length' in the response headers.
我通过fire-debug检查过,响应头中有'Content-Length'。 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. 中间件类在处理请求时自上而下(在
settings.MIDDLEWARE_CLASSES
)应用,在处理响应时自下而上。 If you have 'django.middleware.http.ConditionalGetMiddleware'
in you middleware classes it will add a 'Content-Length'
header to the HttpResponse. 如果你的中间件类中有
'django.middleware.http.ConditionalGetMiddleware'
,它会在HttpResponse中添加一个'Content-Length'
标题。
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. 虽然如果你把你的中间件类放在
settings.MIDDLEWARE_CLASSES
'django.middleware.http.ConditionalGetMiddleware'
,它会在处理响应时先应用这个,然后再应用ConditionalMiddleware
。 That's why you see a Content-Length
header in Firebug, though its not yet processed when you Middleware is called. 这就是为什么你在Firebug中看到
Content-Length
标头的原因,尽管在调用Middleware时它尚未处理。
See Django Middleware documentation for more information about Middleware's. 有关Middleware的更多信息,请参阅Django Middleware文档 。
How about len(response.content)
? 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.
我猜这不一定与字节数相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.