Django - 连续发送输出 [英] Django - Send Output Continually
本文介绍了Django - 连续发送输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从django视图开始处理一些文件,我希望能够在处理文件时将文件的名称发送到浏览器。有没有办法这样做(容易)?我可以使用线程和ajax调用来实现,但是我现在想要最简单的解决方案。
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.
推荐答案
我找到了我需要的在回答从安德烈·米勒提供的链接之一。
I found what I needed in an answer from one of the links that Andre Miller provided.
我发现可以通过一个迭代器到HttpResponse,所以我使用这个代码和它工作:
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())
这篇关于Django - 连续发送输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文