Skip to content
Myr

dbcon.rst

This file

Path

docs/tutorial/dbcon.rst

41 lines · 1.8 KiB

Last changed by

Merge pull request #271 from dafire/masterArmin Ronacherbefore adoption20371b8

History of this file

Take it

The walk stopped after 200 commits: lines older than 63a37b7 point at it.

Improved extension test runnerArmin Ronacherbefore adoption63a37b7

lines 1–10why, one step back
1.. _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.10

Add teardown_request decorator. Fixes issue #174Matt Chisholmbefore adoption04e70bd

line 11why, one step back
11Flask 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 back
12:meth:`~flask.Flask.teardown_request` and :meth:`~flask.Flask.teardown_request`
13decorators::

Improved extension test runnerArmin Ronacherbefore adoption63a37b7

lines 14–18why, one step back
14
15 @app.before_request16 def before_request():17 g.db = connect_db()18

Add teardown_request decorator. Fixes issue #174Matt Chisholmbefore adoption04e70bd

lines 19–22why, one step back
19 @app.teardown_request
20 def teardown_request(exception):21 g.db.close()22

Improved extension test runnerArmin Ronacherbefore adoption63a37b7

line 23why, one step back
23Functions marked with :meth:`~flask.Flask.before_request` are called before

Add teardown_request decorator. Fixes issue #174Matt Chisholmbefore adoption04e70bd

line 24why, one step back
24a request and passed no arguments. Functions marked with

Merge pull request #271 from dafire/masterArmin Ronacherbefore adoption20371b8

line 25why, one step back
25:meth:`~flask.Flask.after_request` are called after a request and

Improved extension test runnerArmin Ronacherbefore adoption63a37b7

line 26why, one step back
26passed the response that will be sent to the client. They have to return

Merge branch 'new-request-dispatching' into blueprintsArmin Ronacherbefore adoption673fa18

lines 27–29why, one step back
27that response object or a different one. They are however not guaranteed
28to be executed if an exception is raised, this is where functions marked with29:meth:`~flask.Flask.teardown_request` come in. They get called after the

Add teardown_request decorator. Fixes issue #174Matt Chisholmbefore adoption04e70bd

lines 30–31why, one step back
30response has been constructed. They are not allowed to modify the request, and
31their return values are ignored. If an exception occurred while the request was

Merge branch 'new-request-dispatching' into blueprintsArmin Ronacherbefore adoption673fa18

line 32why, one step back
32being processed, it is passed to each function; otherwise, `None` is passed in.

Improved extension test runnerArmin Ronacherbefore adoption63a37b7

lines 33–34why, one step back
33
34We store our current database connection on the special :data:`~flask.g`

Merge branch 'new-request-dispatching' into blueprintsArmin Ronacherbefore adoption673fa18

line 35why, one step back
35object that Flask provides for us. This object stores information for one

Improved extension test runnerArmin Ronacherbefore adoption63a37b7

lines 36–41why, one step back
36request only and is available from within each function. Never store such
37things 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`.