dbcon.rst
This file
Path
docs/tutorial/dbcon.rst
62 lines · 2.7 KiB
Last changed by
Fixed a broken referenceArmin Ronacherbefore adoption2c9c269
History of this fileTake it
The walk stopped after 200 commits: lines older than 34bbd31 point at it.
Fixed a failing testcaseArmin Ronacherbefore adoption34bbd31
lines 1–20why, 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.1011Flask 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::1415 @app.before_request16 def before_request():17 g.db = connect_db()1819 @app.teardown_request20 def teardown_request(exception):Modernized docs in a few placesArmin Ronacherbefore adoption29bda59
lines 21–23why, one step back21 db = getattr(g, 'db', None)22 if db is not None:23 db.close()Fixed a failing testcaseArmin Ronacherbefore adoption34bbd31
lines 24–41why, one step back2425Functions marked with :meth:`~flask.Flask.before_request` are called before26a request and passed no arguments. Functions marked with27:meth:`~flask.Flask.after_request` are called after a request and28passed the response that will be sent to the client. They have to return29that response object or a different one. They are however not guaranteed30to be executed if an exception is raised, this is where functions marked with31:meth:`~flask.Flask.teardown_request` come in. They get called after the32response has been constructed. They are not allowed to modify the request, and33their return values are ignored. If an exception occurred while the request was34being processed, it is passed to each function; otherwise, `None` is passed in.3536We store our current database connection on the special :data:`~flask.g`37object that Flask provides for us. This object stores information for one38request only and is available from within each function. Never store such39things on other objects because this would not work with threaded40environments. That special :data:`~flask.g` object does some magic behind41the scenes to ensure it does the right thing.Modernized docs in a few placesArmin Ronacherbefore adoption29bda59
line 42why, one step back42Fixed a broken referenceArmin Ronacherbefore adoption2c9c269
line 43why, one step back43For an even better way to handle such resources see the :ref:`sqlite3`Modernized docs in a few placesArmin Ronacherbefore adoption29bda59
line 44why, one step back44documentation.Fixed a failing testcaseArmin Ronacherbefore adoption34bbd31
lines 45–62why, one step back4546Continue to :ref:`tutorial-views`.4748.. hint:: Where do I put this code?4950 If you've been following along in this tutorial, you might be wondering51 where to put the code from this step and the next. A logical place is to52 group these module-level functions together, and put your new53 ``before_request`` and ``teardown_request`` functions below your existing54 ``init_db`` function (following the tutorial line-by-line).5556 If you need a moment to find your bearings, take a look at how the `example57 source`_ is organized. In Flask, you can put all of your application code58 into a single Python module. You don't have to, and if your app :ref:`grows59 larger <larger-applications>`, it's a good idea not to.6061.. _example source:62 http://github.com/mitsuhiko/flask/tree/master/examples/flaskr/