django - iterating over return render_to_response

363 views Asked by At

I would like to read a file, update the website, read more lines, update the site, etc ...The logic is below but it's not working. It only shows the first line from the logfile and stops. Is there a way to iterate over 'return render_to_response'?

#django view calling a remote python script that appends output to the logfile

proc = subprocess.Popen([program, branch, service, version, nodelist])
logfile = 'text.log'
fh = open(logfile, 'r')

while proc.poll() == None:
  where = fh.tell()
  line = fh.read()
  if not line:
     time.sleep(1)
     fh.seek(where,os.SEEK_SET)
  else:
     output = cgi.escape(line)
     output = line.replace('\n\r', '<br>')
     return render_to_response('hostinfo/deployservices.html', {'response': output})

Thank you for your help.

3

There are 3 answers

0
Burhan Khalid On

Instead of re-inventing the wheel, use django_logtail

2
zaphod On

render_to_response will render the first batch to the website and stop. Then the website must call this view again somehow if you want to send the next batch. You will also have to maintain a record of where you were in the log file so that the second batch can be read from that point.
I assume that you have some logic in the templates so that the second post to render_to_response doesnt overwrite the first
If your data is not humongous, you should explore sending over the entire contents you want to show on the webpage each time you read some new lines.

2
Daniel Roseman On

You can actually do this, by making your function a generator - that is, using 'yield' to return each line.

However, you would need to create the response directly, rather than using render to response.