Skip to content

Instantly share code, notes, and snippets.

@mitsuhiko
Created June 27, 2012 10:58
Show Gist options
  • Save mitsuhiko/3003321 to your computer and use it in GitHub Desktop.
Save mitsuhiko/3003321 to your computer and use it in GitHub Desktop.
Debugging middleware caught exception in streamed response at a point where response headers were already sent.
Traceback (most recent call last):
File "/home/max/.virtualenvs/flask/lib/python2.7/site-packages/werkzeug/wsgi.py", line 513, in next
return self._next()
File "/home/max/.virtualenvs/flask/lib/python2.7/site-packages/werkzeug/wrappers.py", line 829, in iter_encoded
for item in self.response:
File "/home/max/.virtualenvs/flask/lib/python2.7/site-packages/jinja2/environment.py", line 918, in generate
yield self.environment.handle_exception(exc_info, True)
File "/home/max/Projekte/flask-testing-stuff/templates/index.html", line 1, in top-level template code
{% extends "layout.html" %}
File "/home/max/Projekte/flask-testing-stuff/templates/layout.html", line 8, in top-level template code
<a href="{{ url_for('index') }}" class=""><h1>d'Anjou</h1></a>
File "/home/max/.virtualenvs/flask/lib/python2.7/site-packages/flask/helpers.py", line 192, in url_for
blueprint_name = request.blueprint
File "/home/max/.virtualenvs/flask/lib/python2.7/site-packages/werkzeug/local.py", line 336, in __getattr__
return getattr(self._get_current_object(), name)
File "/home/max/.virtualenvs/flask/lib/python2.7/site-packages/werkzeug/local.py", line 295, in _get_current_object
return self.__local()
File "/home/max/.virtualenvs/flask/lib/python2.7/site-packages/flask/globals.py", line 19, in _lookup_object
raise RuntimeError('working outside of request context')
RuntimeError: working outside of request context
{% extends "layout.html" %}
{% block body %}
<ul>
{% for elem in mygenerator %}
<li>
<a href="{{ url_for('index') }}">{{ elem }}</a>
</li>
{% endfor %}
</ul>
{% endblock %}
<!DOCTYPE html>
<head>
<meta charset="utf-8" />
<title>Title</title>
</head>
<body>
<p>
<a href="{{ url_for('index') }}" class=""><h1>d'Anjou</h1></a>
</p>
{% block body %}
{% endblock %}
</body>
</html>
from flask import Flask, Response, _request_ctx_stack
from jinja2 import Template
from time import sleep
app = Flask(__name__)
def get_generator():
with _request_ctx_stack.top:
for i in xrange(10):
sleep(0.5)
yield i
@app.before_request
def before_request(*args):
print 'before_request'
@app.after_request
def after_request(response):
print 'after_request'
return response
@app.teardown_request
def teardown_request(error=None):
print 'teardown_request'
@app.route('/')
def index():
t = app.jinja_env.get_template('index.html')
return Response(t.generate(mygenerator=get_generator()))
if __name__ == "__main__":
app.run(debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment