tags:

views:

58

answers:

4

I want to start processing some files from a django view and I want to be able to send the name of the files to the browser as they are processed. Is there a way to do this (easily)? I could probably do this using threads and ajax calls, but I want the simplest solution for now.

A: 

If you clear the output buffer, then you should be able to see what has been processed.

Ólafur Waage
I'm not sure if this is relevant to a Django web app, views render their output completely before it is passed back to the browser.
Andre Miller
+2  A: 

You would need to use some sort of queuing process if you want to kick off the task when the view is rendered, otherwise the process will finish first before anything is returned to the browser.

Once the task is running asynchronously you could use either AJAX to update the page with the latest status or simply use a meta-refresh inside the page to load the new content.

There is Django queue server here you could use:

http://code.google.com/p/django-queue-service/

It would seem that this question has also been asked a few times before:

Andre Miller
This is the 'correct' method, but it takes time, and I just need a quick-n-dirty solution for now. Turns out one of the links that you provided contained the answer that I needed.
rslite
A: 

First of all, make sure you output a Connection: Keep-Alive header, after which you just have to make sure that the script output isn't being buffered. In Python, you can use the cgi module's cgiprint function to ensure that Python's buffer is cleared, but you should also check the web server configuration, as some will buffer all output until the script finishes running.

pavpanchekha
He's using Django, not cgi scripts
Andre Miller
+1  A: 

I found what I needed in an answer from one of the links that Andre Miller provided.

I found out that's possible to pass an iterator to HttpResponse so I used this code and it worked:

def import_iter():
 """ Used to return output as it is generated """
 # First return the template
 t = loader.get_template('main/qimport.htm')
 c = Context()
 yield t.render(c)
 # Now process the files
 if req.method == 'POST':
  location = req.POST['location']
  if location:
   for finfo in import_location(location):
    yield finfo+"<br/>"

return HttpResponse(import_iter())
rslite