16. Useful Internals¶
RequestContext(app, environ, request=None)¶
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.
When the request context is popped, it will evaluate all the functions registered on the application for teardown execution (
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
DEBUGmode. By setting
Trueon the WSGI environment the context will not pop itself at the end of the request. This is used by the
test_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.
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.
Can be overridden by a subclass to hook into the matching of the request.
Pops the request context and unbinds it by doing that. This will also trigger the execution of functions registered by the
Changed in version 0.9: Added the exc argument.
Binds the request context to the current context.
LocalStackthat 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:
- the active Flask application.
- the URL adapter that was used to match the request.
- the current request object.
- the active session object.
- an object with all the attributes of the
- an internal cache for the flashed messages.
from flask import _request_ctx_stack def get_session(): ctx = _request_ctx_stack.top if ctx is not None: return ctx.session
The application context binds an application object implicitly to the current thread or greenlet, similar to how the
RequestContextbinds 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.
Pops the app context.
Binds the app context to the current context.
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.
BlueprintSetupState(blueprint, app, options, first_registration)¶
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)¶
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.
a reference to the current application
a reference to the blueprint that created this setup state.
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.
The subdomain that the blueprint should be active for,
A dictionary with URL defaults that is added to each and every URL that was defined with the blueprint.
The prefix that should be used for all URLs defined on the blueprint.