16. Useful Internals¶
-
class
flask.ctx.
RequestContext
(app, environ, request=None)[source]¶ The request context contains all request relevant information. It is created at the beginning of the request and pushed to the _request_ctx_stack and removed at the end of it. It will create the URL adapter and request object for the WSGI environment provided.
Do not attempt to use this class directly, instead use
test_request_context()
andrequest_context()
to create this object.When the request context is popped, it will evaluate all the functions registered on the application for teardown execution (
teardown_request()
).The request context is automatically popped at the end of the request for you. In debug mode the request context is kept around if exceptions happen so that interactive debuggers have a chance to introspect the data. With 0.4 this can also be forced for requests that did not fail and outside of
DEBUG
mode. By setting'flask._preserve_context'
toTrue
on the WSGI environment the context will not pop itself at the end of the request. This is used by thetest_client()
for example to implement the deferred cleanup functionality.You might find this helpful for unittests where you need the information from the context local around for a little longer. Make sure to properly
pop()
the stack yourself in that situation, otherwise your unittests will leak memory.-
copy
()[source]¶ Creates a copy of this request context with the same request object. This can be used to move a request context to a different greenlet. Because the actual request object is the same this cannot be used to move a request context to a different thread unless access to the request object is locked.
New in version 0.10.
-
pop
(exc=<object object>)[source]¶ Pops the request context and unbinds it by doing that. This will also trigger the execution of functions registered by the
teardown_request()
decorator.Changed in version 0.9: Added the exc argument.
-
-
flask.
_request_ctx_stack
¶ The internal
LocalStack
that is used to implement all the context local objects used in Flask. This is a documented instance and can be used by extensions and application code but the use is discouraged in general.The following attributes are always present on each layer of the stack:
- app
- the active Flask application.
- url_adapter
- the URL adapter that was used to match the request.
- request
- the current request object.
- session
- the active session object.
- g
- an object with all the attributes of the
flask.g
object. - flashes
- an internal cache for the flashed messages.
Example usage:
from flask import _request_ctx_stack def get_session(): ctx = _request_ctx_stack.top if ctx is not None: return ctx.session
-
class
flask.ctx.
AppContext
(app)[source]¶ The application context binds an application object implicitly to the current thread or greenlet, similar to how the
RequestContext
binds request information. The application context is also implicitly created if a request context is created but the application is not on top of the individual application context.
-
flask.
_app_ctx_stack
¶ Works similar to the request context but only binds the application. This is mainly there for extensions to store data.
New in version 0.9.
-
class
flask.blueprints.
BlueprintSetupState
(blueprint, app, options, first_registration)[source]¶ Temporary holder object for registering a blueprint with the application. An instance of this class is created by the
make_setup_state()
method and later passed to all register callback functions.-
add_url_rule
(rule, endpoint=None, view_func=None, **options)[source]¶ A helper method to register a rule (and optionally a view function) to the application. The endpoint is automatically prefixed with the blueprint’s name.
-
app
= None¶ a reference to the current application
-
blueprint
= None¶ a reference to the blueprint that created this setup state.
-
first_registration
= None¶ as blueprints can be registered multiple times with the application and not everything wants to be registered multiple times on it, this attribute can be used to figure out if the blueprint was registered in the past already.
-
options
= None¶ a dictionary with all options that were passed to the
register_blueprint()
method.
-
subdomain
= None¶ The subdomain that the blueprint should be active for,
None
otherwise.
-
url_defaults
= None¶ A dictionary with URL defaults that is added to each and every URL that was defined with the blueprint.
-
url_prefix
= None¶ The prefix that should be used for all URLs defined on the blueprint.
-