Django flash reaction?

I am sending an AJAX request to a Django view, which can take a lot of time. However, it takes some clearly defined steps, so I would like to print status indicators so that the user knows when he has finished doing something specific, and proceed to the next.

If I used PHP, it might look like this using the flush function:

do_something(); print 'Done doing something!'; flush(); do_something_else(); print 'Done doing something else!'; flush(); 

How will I do the same with Django? Looking at the documentation , I see that HttpResponse objects have a flush method, but all he has to say is that "this method makes an instance of HttpResponse a file-like object." “I'm not sure I want to.” I find it difficult to turn my head around how this can be done in Django, since I have to return the answer and really can not control when the content goes to the browser.

+6
python django
source share
2 answers

Most web servers (e.g. FCGI / SCGI) do their own buffering, HTTP clients do their own, etc. It is very difficult to actually get the data uploaded this way, and in order for the client to actually receive it, because this is not a typical operation.

The closest thing to what you are trying to do is pass HttpResponse to the iterator and do the work in the generator; something like that:

 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 will not necessarily be short-lived. (Not verified code, but you get this idea, see http://docs.djangoproject.com/en/dev/ref/request-response/#passing-iterators .)

Most of the infrastructure simply does not expect a phased response. Even if Django is not buffered, it could be your external server, and probably the client too. That is why in most cases additional updates are required for this: a separate interface for requesting the status of a long-term request.

(I would like to be able to do reliable push updates for this kind of thing ...)

+8
source share

I'm not sure if you need to use the flush () function.

Your AJAX request should just go to the django view.

If your steps can be broken down, save them and create a view for each step. Thus, one process is completed, you can update the user and run the next request through AJAX.

views.py

 def do_something(request): # stuff here return HttpResponse() def do_something_else(request): # more stuff return HttpResponse() 
+4
source share

All Articles