Skip to content
Myr

dbcon.rst

This file

Path

docs/tutorial/dbcon.rst

57 lines · 2.6 KiB

Last changed by

Merge branch 'master' of github.com:mitsuhiko/flaskArmin Ronacherbefore adoption79aa4ac

History of this file

Take it

The walk stopped after 200 commits: lines older than 8ed051d point at it.

Fixed a terrible wrong part in the module documentationArmin Ronacherbefore adoption8ed051d

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 'master' of github.com:mitsuhiko/flaskArmin Ronacherbefore adoptionf8b9f4d

line 12why, one step back
12:meth:`~flask.Flask.after_request` and :meth:`~flask.Flask.teardown_request`

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

line 13why, one step back
13decorators::

Fixed a terrible wrong part in the module documentationArmin Ronacherbefore adoption8ed051d

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

Fixed a terrible wrong part in the module documentationArmin Ronacherbefore adoption8ed051d

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

Fixed a terrible wrong part in the module documentationArmin Ronacherbefore adoption8ed051d

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.

Fixed a terrible wrong part in the module documentationArmin Ronacherbefore adoption8ed051d

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

Fixed a terrible wrong part in the module documentationArmin Ronacherbefore adoption8ed051d

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`.

Merge branch 'master' of github.com:mitsuhiko/flaskArmin Ronacherbefore adoption79aa4ac

lines 42–57why, one step back
42
43.. hint:: Where do I put this code?4445 If you've been following along in this tutorial, you might be wondering46 where to put the code from this step and the next. A logical place is to47 group these module-level functions together, and put your new48 ``before_request`` and ``teardown_request`` functions below your existing49 ``init_db`` function (following the tutorial line-by-line).5051 If you need a moment to find your bearings, take a look at how the `example52 source`_ is organized. In Flask, you can put all of your application code53 into a single Python module. You don't have to, and if your app :ref:`grows54 larger <larger-applications>`, it's a good idea not to.5556.. _example source:57 http://github.com/mitsuhiko/flask/tree/master/examples/flaskr/