Modernized docs in a few places
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent e2d4aa1 · Browse files · 3 files·+29 added−18 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+29 added−18 removed
- other:+29 added−18 removed
Provenance
- Commit
29bda59- Tree
c895e82- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
e2d4aa1
3 files changed+29 added−18 removed
modified:docs/patterns/sqlite3.rst+12 added-8 removed
8 unmodified lines9 10 Here is a simple example of how you can use SQLite 3 with Flask::11 12 import sqlite313- from flask import _app_ctx_stack13+ from flask import g14 15 DATABASE = '/path/to/database.db'16 17 def get_db():18- top = _app_ctx_stack.top19- if not hasattr(top, 'sqlite_db'):20- top.sqlite_db = sqlite3.connect(DATABASE)21- return top.sqlite_db18+ db = getattr(g, '_database', None)19+ if db is None:20+ db = g._database = connect_to_database()21+ return db22 23 @app.teardown_appcontext24 def close_connection(exception):25- top = _app_ctx_stack.top26- if hasattr(top, 'sqlite_db'):27- top.sqlite_db.close()25+ db = getattr(g, '_database', None)26+ if db is not None:27+ db.close()28 29 All the application needs to do in order to now use the database is having30 an active application context (which is always true if there is an request31 in flight) or to create an application context itself. At that point the32 ``get_db`` function can be used to get the current database connection.33 Whenever the context is destroyed the database connection will be34 terminated.35 36+Note: if you use Flask 0.9 or older you need to use37+``flask._app_ctx_stack.top`` instead of ``g`` as the :data:`flask.g`38+object was bound to the request and not application context.39+40 Example::41 42 @app.route('/')43 def index():95 unmodified linesmodified:docs/tutorial/dbcon.rst+6 added-1 removed
16 unmodified lines17 g.db = connect_db()18 19 @app.teardown_request20 def teardown_request(exception):21+ db = getattr(g, 'db', None)22+ if db is not None:21- g.db.close()23+ db.close()24 25 Functions marked with :meth:`~flask.Flask.before_request` are called before26 a request and passed no arguments. Functions marked with27 :meth:`~flask.Flask.after_request` are called after a request and11 unmodified lines39 things on other objects because this would not work with threaded40 environments. That special :data:`~flask.g` object does some magic behind41 the scenes to ensure it does the right thing.42 43+For an even better way to handle such resources see the :ref:`slite3`44+documentation.45+46 Continue to :ref:`tutorial-views`.47 48 .. hint:: Where do I put this code?49 13 unmodified linesmodified:docs/appcontext.rst+11 added-9 removed
80 unmodified lines81 moves between threads and it will not be shared between requests. As such82 it is the perfect place to store database connection information and other83 things. The internal stack object is called :data:`flask._app_ctx_stack`.84 Extensions are free to store additional information on the topmost level,85-assuming they pick a sufficiently unique name.85+assuming they pick a sufficiently unique name and should put there86+information there, instead on the :data:`flask.g` object which is reserved87+for user code.88 89 For more information about that, see :ref:`extension-dev`.90 91 Context Usage16 unmodified lines108 109 This is an example that connects to a database::110 111 import sqlite3110- from flask import _app_ctx_stack112+ from flask import g113 114 def get_db():113- top = _app_ctx_stack.top114- if not hasattr(top, 'database'):115- top.database = connect_to_database()116- return top.database115+ db = getattr(g, '_database', None)116+ if db is None:117+ db = g._database = connect_to_database()118+ return db119 120 @app.teardown_appcontext121 def teardown_db(exception):120- top = _app_ctx_stack.top121- if hasattr(top, 'database'):122- top.database.close()122+ db = getattr(g, '_database', None)123+ if db is not None:124+ db.close()125 126 The first time ``get_db()`` is called the connection will be established.127 To make this implicit a :class:`~werkzeug.local.LocalProxy` can be used::128 5 unmodified lines