Skip to content
Myr

deferredcallbacks.rst

This file

Path

docs/patterns/deferredcallbacks.rst

45 lines · 1.8 KiB

Last changed by

Merge pull request #2279 from davidism/docs-after_this_requestDavid Lordbefore adoption716edfd

History of this file

Take it

The walk stopped after 200 commits: lines older than a228a3b point at it.

Merge pull request #1862 from antsar/masterJeff Widmanbefore adoptiona228a3b

lines 1–5why, one step back
1.. _deferred-callbacks:
23Deferred Request Callbacks4==========================5

Merge pull request #2279 from davidism/docs-after_this_requestDavid Lordbefore adoption716edfd

lines 6–10why, one step back
6One of the design principles of Flask is that response objects are created and
7passed down a chain of potential callbacks that can modify them or replace8them. When the request handling starts, there is no response object yet. It is9created as necessary either by a view function or by some other component in10the system.

Merge pull request #1862 from antsar/masterJeff Widmanbefore adoptiona228a3b

line 11why, one step back
11

Merge pull request #2279 from davidism/docs-after_this_requestDavid Lordbefore adoption716edfd

lines 12–15why, one step back
12What happens if you want to modify the response at a point where the response
13does not exist yet? A common example for that would be a14:meth:`~flask.Flask.before_request` callback that wants to set a cookie on the15response object.

Merge pull request #1862 from antsar/masterJeff Widmanbefore adoptiona228a3b

line 16why, one step back
16

Merge pull request #2279 from davidism/docs-after_this_requestDavid Lordbefore adoption716edfd

lines 17–20why, one step back
17One way is to avoid the situation. Very often that is possible. For instance
18you can try to move that logic into a :meth:`~flask.Flask.after_request`19callback instead. However, sometimes moving code there makes it more20more complicated or awkward to reason about.

Merge pull request #1862 from antsar/masterJeff Widmanbefore adoptiona228a3b

line 21why, one step back
21

Merge pull request #2279 from davidism/docs-after_this_requestDavid Lordbefore adoption716edfd

lines 22–25why, one step back
22As an alternative, you can use :func:`~flask.after_this_request` to register
23callbacks that will execute after only the current request. This way you can24defer code execution from anywhere in the application, based on the current25request.

Merge pull request #1862 from antsar/masterJeff Widmanbefore adoptiona228a3b

lines 26–27why, one step back
26
27At any time during a request, we can register a function to be called at the

Merge pull request #2279 from davidism/docs-after_this_requestDavid Lordbefore adoption716edfd

lines 28–29why, one step back
28end of the request. For example you can remember the current language of the
29user in a cookie in a :meth:`~flask.Flask.before_request` callback::

Merge pull request #1862 from antsar/masterJeff Widmanbefore adoptiona228a3b

line 30why, one step back
30

Merge pull request #1965 from vioan/masterDavid Lordbefore adoption0420cb2

line 31why, one step back
31 from flask import request, after_this_request

Merge pull request #1862 from antsar/masterJeff Widmanbefore adoptiona228a3b

lines 32–35why, one step back
32
33 @app.before_request34 def detect_user_language():35 language = request.cookies.get('user_lang')

Merge pull request #2279 from davidism/docs-after_this_requestDavid Lordbefore adoption716edfd

line 36why, one step back
36

Merge pull request #1862 from antsar/masterJeff Widmanbefore adoptiona228a3b

lines 37–38why, one step back
37 if language is None:
38 language = guess_language_from_request()

Merge pull request #2279 from davidism/docs-after_this_requestDavid Lordbefore adoption716edfd

lines 39–40why, one step back
39
40 # when the response exists, set a cookie with the language

Merge pull request #1862 from antsar/masterJeff Widmanbefore adoptiona228a3b

lines 41–43why, one step back
41 @after_this_request
42 def remember_language(response):43 response.set_cookie('user_lang', language)

Merge pull request #2279 from davidism/docs-after_this_requestDavid Lordbefore adoption716edfd

line 44why, one step back
44

Merge pull request #1862 from antsar/masterJeff Widmanbefore adoptiona228a3b

line 45why, one step back
45 g.language = language