dbcon.rst
This file
Path
docs/tutorial/dbcon.rst
78 lines · 3.3 KiB
Last changed by
Merge pull request #886 from talam/patch-1Daniel Neuhäuserbefore adoptioncab514e
History of this fileTake it
The walk stopped after 200 commits: lines older than b31f2d9 point at it.
Require Werkzeug>=0.7, #449.Ron DuPlainbefore adoptionb31f2d9
lines 1–2why, one step back1.. _tutorial-dbcon:2Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3
lines 3–4why, one step back3Step 3: Database Connections4----------------------------Require Werkzeug>=0.7, #449.Ron DuPlainbefore adoptionb31f2d9
line 5why, one step back5Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3
line 6why, one step back6We have created a function for establishing a database connection withMerge pull request #886 from talam/patch-1Daniel Neuhäuserbefore adoptioncab514e
line 7why, one step back7`connect_db` but by itself that's not particularly useful. Creating andRewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3
lines 8–11why, one step back8closing database connections all the time is very inefficient, so we want9to keep it around for longer. Because database connections encapsulate a10transaction we also need to make sure that only one request at the time11uses the connection. So how can we elegantly do that with Flask?Require Werkzeug>=0.7, #449.Ron DuPlainbefore adoptionb31f2d9
line 12why, one step back12Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3
lines 13–14why, one step back13This is where the application context comes into play. So let's start14there.Require Werkzeug>=0.7, #449.Ron DuPlainbefore adoptionb31f2d9
line 15why, one step back15Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3
lines 16–22why, one step back16Flask provides us with two contexts: the application context and the17request context. For the time being all you have to know is that there18are special variables that use these. For instance the19:data:`~flask.request` variable is the request object associated with20the current request, whereas :data:`~flask.g` is a general purpose21variable associated with the current application context. We will go into22the details of this a bit later.Require Werkzeug>=0.7, #449.Ron DuPlainbefore adoptionb31f2d9
line 23why, one step back23Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3
line 24why, one step back24For the time being all you have to know is that you can store informationMerge pull request #886 from talam/patch-1Daniel Neuhäuserbefore adoptioncab514e
line 25why, one step back25safely on the :data:`~flask.g` object.Require Werkzeug>=0.7, #449.Ron DuPlainbefore adoptionb31f2d9
line 26why, one step back26Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3
lines 27–30why, one step back27So when do you put it on there? To do that you can make a helper28function. The first time the function is called it will create a database29connection for the current context and successive calls will return the30already established connection::Require Werkzeug>=0.7, #449.Ron DuPlainbefore adoptionb31f2d9
line 31why, one step back31Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3
lines 32–38why, one step back32 def get_db():33 """Opens a new database connection if there is none yet for the34 current application context.35 """36 if not hasattr(g, 'sqlite_db'):37 g.sqlite_db = connect_db()38 return g.sqlite_dbModernized docs in a few placesArmin Ronacherbefore adoption29bda59
line 39why, one step back39Require Werkzeug>=0.7, #449.Ron DuPlainbefore adoptionb31f2d9
line 40why, one step back40Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3
lines 41–43why, one step back41So now we know how to connect, but how do we properly disconnect? For42that flask provides us with the :meth:`~flask.Flask.teardown_appcontext`43decorator. It's executed every time the application context tears down::Require Werkzeug>=0.7, #449.Ron DuPlainbefore adoptionb31f2d9
line 44why, one step back44Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3
lines 45–63why, one step back45 @app.teardown_appcontext46 def close_db(error):47 """Closes the database again at the end of the request."""48 if hasattr(g, 'sqlite_db'):49 g.sqlite_db.close()5051Functions marked with :meth:`~flask.Flask.teardown_appcontext` are called52every time the app context tears down. So what does this mean?53Essentially the app context is created before the request comes in and is54destroyed (teared down) whenever the request finishes. A teardown can55happen because of two reasons: either everything went well (the error56parameter will be `None`) or an exception happend in which case the error57is passed to the teardown function.5859Curious about what these contexts mean? Have a look at the60:ref:`app-context` documentation to learn more.6162Continue to :ref:`tutorial-dbinit`.63Require Werkzeug>=0.7, #449.Ron DuPlainbefore adoptionb31f2d9
lines 64–68why, one step back64.. hint:: Where do I put this code?6566 If you've been following along in this tutorial, you might be wondering67 where to put the code from this step and the next. A logical place is to68 group these module-level functions together, and put your newRewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3
line 69why, one step back69 ``get_db`` and ``close_db`` functions below your existingMerge pull request #886 from talam/patch-1Daniel Neuhäuserbefore adoptioncab514e
line 70why, one step back70 ``connect_db`` function (following the tutorial line-by-line).Require Werkzeug>=0.7, #449.Ron DuPlainbefore adoptionb31f2d9
lines 71–78why, one step back7172 If you need a moment to find your bearings, take a look at how the `example73 source`_ is organized. In Flask, you can put all of your application code74 into a single Python module. You don't have to, and if your app :ref:`grows75 larger <larger-applications>`, it's a good idea not to.7677.. _example source:78 http://github.com/mitsuhiko/flask/tree/master/examples/flaskr/