Home / Function/ after_this_request() — flask Function Reference

after_this_request() — flask Function Reference

Architecture documentation for the after_this_request() function in ctx.py from the flask codebase.

Entity Profile

Dependency Diagram

graph TD
  8167ef87_1dbb_573b_1e60_34c9eaef098a["after_this_request()"]
  49f8280b_d7dc_110c_b848_8e7e56bfb19b["ctx.py"]
  8167ef87_1dbb_573b_1e60_34c9eaef098a -->|defined in| 49f8280b_d7dc_110c_b848_8e7e56bfb19b
  c1644887_6834_cf5b_9551_6a1aa07a77d7["get()"]
  8167ef87_1dbb_573b_1e60_34c9eaef098a -->|calls| c1644887_6834_cf5b_9551_6a1aa07a77d7
  style 8167ef87_1dbb_573b_1e60_34c9eaef098a fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

src/flask/ctx.py lines 117–147

def after_this_request(
    f: ft.AfterRequestCallable[t.Any],
) -> ft.AfterRequestCallable[t.Any]:
    """Decorate a function to run after the current request. The behavior is the
    same as :meth:`.Flask.after_request`, except it only applies to the current
    request, rather than every request. Therefore, it must be used within a
    request context, rather than during setup.

    .. code-block:: python

        @app.route("/")
        def index():
            @after_this_request
            def add_header(response):
                response.headers["X-Foo"] = "Parachute"
                return response

            return "Hello, World!"

    .. versionadded:: 0.9
    """
    ctx = _cv_app.get(None)

    if ctx is None or not ctx.has_request:
        raise RuntimeError(
            "'after_this_request' can only be used when a request"
            " context is active, such as in a view function."
        )

    ctx._after_request_functions.append(f)
    return f

Subdomains

Defined In

Calls

Frequently Asked Questions

What does after_this_request() do?
after_this_request() is a function in the flask codebase, defined in src/flask/ctx.py.
Where is after_this_request() defined?
after_this_request() is defined in src/flask/ctx.py at line 117.
What does after_this_request() call?
after_this_request() calls 1 function(s): get.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free