dbcon.rst
This file
Path
docs/tutorial/dbcon.rst
41 lines · 1.8 KiB
Last changed by
Merge branch 'new-request-dispatching' into blueprintsArmin Ronacherbefore adoption673fa18
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
line 11why, one step back11Flask allows us to do that with the :meth:`~flask.Flask.before_request`,Merge branch 'new-request-dispatching' into blueprintsArmin Ronacherbefore adoption673fa18
lines 12–13why, one step back12:meth:`~flask.Flask.teardown_request` and :meth:`~flask.Flask.teardown_request`13decorators::Fixed a typoArmin Ronacherbefore adoption5dd4e9f
lines 14–18why, one step back1415 @app.before_request16 def before_request():17 g.db = connect_db()18Add teardown_request decorator. Fixes issue #174Matt Chisholmbefore adoption04e70bd
lines 19–22why, one step back19 @app.teardown_request20 def teardown_request(exception):21 g.db.close()22Fixed a typoArmin Ronacherbefore adoption5dd4e9f
line 23why, one step back23Functions marked with :meth:`~flask.Flask.before_request` are called beforeAdd teardown_request decorator. Fixes issue #174Matt Chisholmbefore adoption04e70bd
line 24why, one step back24a request and passed no arguments. Functions marked withMerge branch 'new-request-dispatching' into blueprintsArmin Ronacherbefore adoption673fa18
line 25why, one step back25:meth:`~flask.Flask.teardown_request` are called after a request andFixed a typoArmin Ronacherbefore adoption5dd4e9f
line 26why, one step back26passed the response that will be sent to the client. They have to returnMerge branch 'new-request-dispatching' into blueprintsArmin Ronacherbefore adoption673fa18
lines 27–29why, one step back27that response object or a different one. They are however not guaranteed28to be executed if an exception is raised, this is where functions marked with29:meth:`~flask.Flask.teardown_request` come in. They get called after theAdd teardown_request decorator. Fixes issue #174Matt Chisholmbefore adoption04e70bd
lines 30–31why, one step back30response has been constructed. They are not allowed to modify the request, and31their return values are ignored. If an exception occurred while the request wasMerge branch 'new-request-dispatching' into blueprintsArmin Ronacherbefore adoption673fa18
line 32why, one step back32being processed, it is passed to each function; otherwise, `None` is passed in.Fixed a typoArmin Ronacherbefore adoption5dd4e9f
lines 33–34why, one step back3334We store our current database connection on the special :data:`~flask.g`Merge branch 'new-request-dispatching' into blueprintsArmin Ronacherbefore adoption673fa18
line 35why, one step back35object that Flask provides for us. This object stores information for oneFixed a typoArmin Ronacherbefore adoption5dd4e9f
lines 36–41why, one step back36request only and is available from within each function. Never store such37things on other objects because this would not work with threaded38environments. That special :data:`~flask.g` object does some magic behind39the scenes to ensure it does the right thing.4041Continue to :ref:`tutorial-views`.