Modernized docs in a few places

This commit is contained in:
Armin Ronacher 2013-06-09 12:30:27 +01:00
parent e2d4aa1207
commit 29bda590b2
3 changed files with 29 additions and 18 deletions

View file

@ -82,7 +82,9 @@ moves between threads and it will not be shared between requests. As such
it is the perfect place to store database connection information and other
things. The internal stack object is called :data:`flask._app_ctx_stack`.
Extensions are free to store additional information on the topmost level,
assuming they pick a sufficiently unique name.
assuming they pick a sufficiently unique name and should put there
information there, instead on the :data:`flask.g` object which is reserved
for user code.
For more information about that, see :ref:`extension-dev`.
@ -107,19 +109,19 @@ and a ``teardown_X()`` function that is registered as teardown handler.
This is an example that connects to a database::
import sqlite3
from flask import _app_ctx_stack
from flask import g
def get_db():
top = _app_ctx_stack.top
if not hasattr(top, 'database'):
top.database = connect_to_database()
return top.database
db = getattr(g, '_database', None)
if db is None:
db = g._database = connect_to_database()
return db
@app.teardown_appcontext
def teardown_db(exception):
top = _app_ctx_stack.top
if hasattr(top, 'database'):
top.database.close()
db = getattr(g, '_database', None)
if db is not None:
db.close()
The first time ``get_db()`` is called the connection will be established.
To make this implicit a :class:`~werkzeug.local.LocalProxy` can be used::