Python 3.6.3,Django 2.0.3
我是django的新手,但我想创建一个非常简单的网站,有人可以触发一些以前只是misc,独立的python脚本的任务。不幸的是,这些任务可能需要很长时间。我希望能够在以下模板的中间显示这些任务的输出({{ stream }}
所在的位置),以便用户得到一些有意义的反馈。
{% load pagePieces %}
{% page_header %}
<div class="container">
<div class="row">
<a class="btn" href="/"><i class="fa fa-chevron-left"></i> Home</a>
</div>
<div class="row row-header"><h1>{{ operation }}</h1></div>
<div class="row row-content">
{{ stream }}
</div>
</div>
{% page_footer %}
在我的视图文件中,我尝试了一些不同的东西,但这里是关于我现在的位置(这有点简化。我拿出了一些错误处理并更改了一些名称):
def myaction(request):
output_template = loader.get_template('myapp/process_output.html')
return StreamingHttpResponse(
output_template.render({
'operation': 'That long running task',
"stream": streaming_wrapper()
})
)
def streaming_wrapper():
output_template = loader.get_template('myapp/process_output.html')
x = yield from a_module.long_running_task()
yield output_template.render({
'operation': 'That long running task',
"stream": x
})
这会传输来自long_running_task()的输出,但是在完成后才会加载模板的其余部分。在其他方面,我已经在模板之后输出流,但从来没有在中间,这看起来很糟糕,因为我的模板有一个页眉和一个页脚。
我不确定如何使这项工作,我不确定答案是否在我的观点中,或者可能用我的模板做一些更复杂的事情。
(我也知道这与这两个问题相似,但它们都没有令人满意的答案,而且已经好几年了。
Django: return a StreamingHttpResponse on an existing html page
答案 0 :(得分:0)
尝试按照 Miguel Grinberg 的这篇博客实现步骤 - https://blog.miguelgrinberg.com/post/video-streaming-with-flask/page/8(这是用于 Flask)。但是您必须遵循相同的步骤,并且还需要根据需要修改模板。这些步骤对我有用。