Skip to content
Myr

appcontext.rst

This file

Path

docs/appcontext.rst

88 lines · 3.7 KiB

Last changed by

Fixed some smaller things in the docsArmin Ronacherbefore adoptiond26af4f

History of this file

Take it

1.. _app-context:23The Application Context4=======================56.. versionadded:: 0.978One of the design ideas behind Flask is that there are two different9“states” in which code is executed.  The application setup state in which10the application implicitly is on the module level.  It starts when the11:class:`Flask` object is instantiated, and it implicitly ends when the12first request comes in.  While the application is in this state a few13assumptions are true:1415-   the programmer can modify the application object safely.16-   no request handling happened so far17-   you have to have a reference to the application object in order to18    modify it, there is no magic proxy that can give you a reference to19    the application object you're currently creating or modifying.2021On the contrast, during request handling, a couple of other rules exist:2223-   while a request is active, the context local objects24    (:data:`flask.request` and others) point to the current request.25-   any code can get hold of these objects at any time.2627There is a third state which is sitting in between a little bit.28Sometimes you are dealing with an application in a way that is similar to29how you interact with applications during request handling just that there30is no request active.  Consider for instance that you're sitting in an31interactive Python shell and interacting with the application, or a32command line application.3334The application context is what powers the :data:`~flask.current_app`35context local.3637Purpose of the Application Context38----------------------------------3940The main reason for the application's context existance is that in the41past a bunch of functionality was attached to the request context in lack42of a better solution.  Since one of the pillar's of Flask's design is that43you can have more than one application in the same Python process.4445So how does the code find the “right” application?  In the past we46recommended passing applications around explicitly, but that caused issues47with libraries that were not designed with that in mind for libraries for48which it was too inconvenient to make this work.4950A common workaround for that problem was to use the51:data:`~flask.current_app` proxy later on, which was bound to the current52request's application reference.  Since however creating such a request53context is an unnecessarily expensive operation in case there is no54request around, the application context was introduced.5556Creating an Application Context57-------------------------------5859To make an application context there are two ways.  The first one is the60implicit one: whenever a request context is pushed, an application context61will be created alongside if this is necessary.  As a result of that, you62can ignore the existance of the application context unless you need it.6364The second way is the explicit way using the65:meth:`~flask.Flask.app_context` method::6667    from flask import Flask, current_app6869    app = Flask(__name__)70    with app.app_context():71        # within this block, current_app points to app.72        print current_app.name7374The application context is also used by the :func:`~flask.url_for`75function in case a ``SERVER_NAME`` was configured.  This allows you to76generate URLs even in the absence of a request.7778Locality of the Context79-----------------------8081The application context is created and destroyed as necessary.  It never82moves between threads and it will not be shared between requests.  As such83it is the perfect place to store database connection information and other84things.  The internal stack object is called :data:`flask._app_ctx_stack`.85Extensions are free to store additional information on the topmost level,86assuming they pick a sufficiently unique name.8788For more information about that, see :ref:`extension-dev`.