首页 > 代码库 > DJANGO的HTTPRESPONSE流式输出

DJANGO的HTTPRESPONSE流式输出

在项目当中遇到的问题,网上有样例代码,但都不行,后来,发现在了1.5版本之后,新的STREAMHTTPRESPONSE对象,

搞定。

from django.http import HttpResponsefrom django.http import StreamingHttpResponseimport timefrom 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 respdef 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")

DJANGO的HTTPRESPONSE流式输出