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

@ -10,21 +10,21 @@ easily.
Here is a simple example of how you can use SQLite 3 with Flask::
import sqlite3
from flask import _app_ctx_stack
from flask import g
DATABASE = '/path/to/database.db'
def get_db():
top = _app_ctx_stack.top
if not hasattr(top, 'sqlite_db'):
top.sqlite_db = sqlite3.connect(DATABASE)
return top.sqlite_db
db = getattr(g, '_database', None)
if db is None:
db = g._database = connect_to_database()
return db
@app.teardown_appcontext
def close_connection(exception):
top = _app_ctx_stack.top
if hasattr(top, 'sqlite_db'):
top.sqlite_db.close()
db = getattr(g, '_database', None)
if db is not None:
db.close()
All the application needs to do in order to now use the database is having
an active application context (which is always true if there is an request
@ -33,6 +33,10 @@ in flight) or to create an application context itself. At that point the
Whenever the context is destroyed the database connection will be
terminated.
Note: if you use Flask 0.9 or older you need to use
``flask._app_ctx_stack.top`` instead of ``g`` as the :data:`flask.g`
object was bound to the request and not application context.
Example::
@app.route('/')