Python 流\u与\u上下文和流\u模板一起引发运行时错误
我根据以下文件工作: 下面是回溯的一个很好的部分Python 流\u与\u上下文和流\u模板一起引发运行时错误,python,python-2.7,flask,Python,Python 2.7,Flask,我根据以下文件工作: 下面是回溯的一个很好的部分 File "(my template)", line 85, in block "scripts" {{ super() }} File "/usr/local/lib/python2.7/site-packages/flask_bootstrap/templates/bootstrap/base.html", line 27, in block "scripts" <script src="{{bootstrap_
File "(my template)", line 85, in block "scripts"
{{ super() }}
File "/usr/local/lib/python2.7/site-packages/flask_bootstrap/templates/bootstrap/base.html", line 27, in block "scripts"
<script src="{{bootstrap_find_resource('jquery.js', cdn='jquery')}}"></script>
File "/usr/local/lib/python2.7/site-packages/flask_bootstrap/__init__.py", line 93, in bootstrap_find_resource
config = current_app.config
File "/usr/local/lib/python2.7/site-packages/werkzeug/local.py", line 338, in __getattr__
return getattr(self._get_current_object(), name)
File "/usr/local/lib/python2.7/site-packages/werkzeug/local.py", line 297, in _get_current_object
return self.__local()
File "/usr/local/lib/python2.7/site-packages/flask/globals.py", line 34, in _find_app
raise RuntimeError('working outside of application context')
虽然我注意到文档中明确指出,如果没有stream\u with\u context()函数,
,那么此时会出现运行时错误。
,我不明白为什么这会导致运行时错误,尽管已经有一个包含上下文的stream\u我不完全熟悉stream\u模板
方法中的操作,但是根据示例,您需要在Response
的第一个参数上使用stream\u with\u context
,而不是围绕模板参数
如果需要基于模板进行流式处理,那么该模板可能需要是generate函数的一部分
@app.route('/render', methods=['GET', 'POST'])
def render():
form = MyForm(request.form)
if request.method == 'POST'
def generate():
for i,v in enumerate(my_data):
yield (i,v)
return Response(stream_template('results.html'), form=form, results=stream_with_context(generate))
else:
return render_template('advanced.html')
def stream_template(template_name, **context):
app.update_template_context(context)
t = app.jinja_env.get_template(template_name)
rv = t.stream(context)
rv.enable_buffering(5)
return rv