I am sending an AJAX request to a Django view that can potentially take a lot of time. It goes through some well-defined steps, however, so I would like to print status indi
Most webservers (eg. FCGI/SCGI) do their own buffering, HTTP clients do their own, and so on. It's very difficult to actually get data flushed out in this way and for the client to actually receive it, because it's not a typical operation.
The closest to what you're trying to do would be to pass an iterator to HttpResponse, and to do the work in a generator; something like this:
def index(request):
def do_work():
step_1()
yield "step 1 complete"
step_2()
yield "step 2 complete"
step_3()
yield "step 3 complete"
return HttpResponse(do_work())
... but this won't necessarily flush. (Not tested code, but you get the idea; see http://docs.djangoproject.com/en/dev/ref/request-response/#passing-iterators.)
Most of the infrastructure is simply not expecting a piecemeal response. Even if Django isn't buffering, your front-end server might be, and the client probably is, too. That's why most things use pull updates for this: a separate interface to query the status of a long-running request.
(I'd like to be able to do reliable push updates for this sort of thing, too...)