在项目当中遇到的问题,网上有样例代码,但都不行,后来,发现在了1.5版本之后,新的STREAMHTTPRESPONSE对象,
搞定。
from django.http import HttpResponse from django.http import StreamingHttpResponse import time from django.views.decorators.http import condition @condition(etag_func=None) def stream_response(request): #resp = HttpResponse( stream_response_generator(), mimetype=‘text/html‘) resp = StreamingHttpResponse( stream_response_generator(), ) return resp def stream_response_generator(): yield "<html><body>\n" for x in range(1,15): yield "<div>%s second putput</div>\n" % x yield " " * 1024 # Encourage browser to render incrementally time.sleep(1) yield "</body></html>\n" def hello(request): return HttpResponse("hello world")
时间: 2024-10-09 20:44:48