你可以使用条件装饰器禁用ETAG中间件。这将使你的响应通过HTTP流回。你可以使用命令行工具(如)进行确认
curl。但这可能不足以让你的浏览器在流式传输时显示响应。为了鼓励浏览器在流式传输时显示响应,可以在管道中向下推动一堆空白以强制其缓冲区填充。示例如下:
from django.views.decorators.http import condition@condition(etag_func=None)def stream_response(request): resp = HttpResponse( stream_response_generator(), content_type='text/html') return respdef stream_response_generator(): yield "<html><body>n" for x in range(1,11): yield "<div>%s</div>n" % x yield " " * 1024 # Encourage browser to render incrementally time.sleep(1) yield "</body></html>n"



