Skip to content
Myr

Merge pull request #884 from rsyring/patch-2

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Update appfactories.rst, make extension related section clearer

authored by Armin Ronacher · committed by Armin Ronacher · parents a950358, fc85bf4 · Browse files · 1 file+28 added−10 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+28 added−10 removed

  • other:+28 added−10 removed

1 file+28 added−10 removed

modified:docs/patterns/appfactories.rst+28 added-10 removed
52 unmodified lines
53 return render_template(current_app.config['INDEX_TEMPLATE'])5455Here we look up the name of a template in the config.5657Extension objects are not initially bound to an application. Using58``db.init_app``, the app gets configured for the extension. No59application-specific state is stored on the extension object, so one extension60object can be used for multiple apps. For more information about the design of61extensions refer to :doc:`/extensiondev`.57Factories & Extensions58----------------------5963Your `model.py` might look like this when using `Flask-SQLAlchemy64<http://pythonhosted.org/Flask-SQLAlchemy/>`_::60It's preferable to create your extensions and app factories so that the61extension object does not initially get bound to the application.6266 from flask.ext.sqlalchemy import SQLAlchemy67 # no app object passed! Instead we use use db.init_app in the factory.63Using `Flask-SQLAlchemy <http://pythonhosted.org/Flask-SQLAlchemy/>`_, 64as an example, you should **not** do::65 66 def create_app(config_filename):67 app = Flask(__name__)68 app.config.from_pyfile(config_filename)6970 db = SQLAlchemy(app)7172But, rather, in model.py (or equivalent)::7374 db = SQLAlchemy()75 76and in your application.py (or equivalent)::7770 # create some models78 def create_app(config_filename):79 app = Flask(__name__)80 app.config.from_pyfile(config_filename)8182 from yourapplication.model import db83 db.init_app(app)848586Using this design pattern, no application-specific state is stored on the87extension object, so one extension object can be used for multiple apps. 88For more information about the design of extensions refer to :doc:`/extensiondev`.8990Using Applications91------------------9293So to use such an application you then have to create the application
18 unmodified lines