dbcon.rst
This file
Path
docs/tutorial/dbcon.rst
56 lines · 2.3 KiB
Last changed by
Add teardown_request decorator. Fixes issue #174Matt Chisholmbefore adoption04e70bd
History of this fileTake it
The walk stopped after 200 commits: lines older than 5dd4e9f point at it.
Fixed a typoArmin Ronacherbefore adoption5dd4e9f
lines 1–10why, one step back1.. _tutorial-dbcon:23Step 4: Request Database Connections4------------------------------------56Now we know how we can open database connections and use them for scripts,7but how can we elegantly do that for requests? We will need the database8connection in all our functions so it makes sense to initialize them9before each request and shut them down afterwards.10Add teardown_request decorator. Fixes issue #174Matt Chisholmbefore adoption04e70bd
lines 11–15why, one step back11Flask allows us to do that with the :meth:`~flask.Flask.before_request`,12:meth:`~flask.Flask.after_request` and :meth:`~flask.Flask.teardown_request`13decorators. In debug mode, if an error is raised,14:meth:`~flask.Flask.after_request` won't be run, and you'll have access to the15db connection in the interactive debugger::Fixed a typoArmin Ronacherbefore adoption5dd4e9f
lines 16–25why, one step back1617 @app.before_request18 def before_request():19 g.db = connect_db()2021 @app.after_request22 def after_request(response):23 g.db.close()24 return response25Add teardown_request decorator. Fixes issue #174Matt Chisholmbefore adoption04e70bd
lines 26–36why, one step back26If you want to guarantee that the connection is always closed in debug mode, you27can close it in a function decorated with :meth:`~flask.Flask.teardown_request`:2829 @app.before_request30 def before_request():31 g.db = connect_db()3233 @app.teardown_request34 def teardown_request(exception):35 g.db.close()36Fixed a typoArmin Ronacherbefore adoption5dd4e9f
line 37why, one step back37Functions marked with :meth:`~flask.Flask.before_request` are called beforeAdd teardown_request decorator. Fixes issue #174Matt Chisholmbefore adoption04e70bd
line 38why, one step back38a request and passed no arguments. Functions marked withFixed a typoArmin Ronacherbefore adoption5dd4e9f
lines 39–42why, one step back39:meth:`~flask.Flask.after_request` are called after a request and40passed the response that will be sent to the client. They have to return41that response object or a different one. In this case we just return it42unchanged.Add teardown_request decorator. Fixes issue #174Matt Chisholmbefore adoption04e70bd
lines 43–47why, one step back4344Functions marked with :meth:`~flask.Flask.teardown_request` get called after the45response has been constructed. They are not allowed to modify the request, and46their return values are ignored. If an exception occurred while the request was47being processed, it is passed to each function; otherwise, None is passed in.Fixed a typoArmin Ronacherbefore adoption5dd4e9f
lines 48–56why, one step back4849We store our current database connection on the special :data:`~flask.g`50object that flask provides for us. This object stores information for one51request only and is available from within each function. Never store such52things on other objects because this would not work with threaded53environments. That special :data:`~flask.g` object does some magic behind54the scenes to ensure it does the right thing.5556Continue to :ref:`tutorial-views`.