Skip to content
Myr

deferredcallbacks.rst

This file

Path

docs/patterns/deferredcallbacks.rst

45 lines · 1.8 KiB

Last changed by

Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89

History of this file

Take it

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

Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d

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

Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89

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 #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d

line 11why, one step back
11

Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89

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 #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d

line 16why, one step back
16

Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89

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 #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d

line 21why, one step back
21

Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89

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 #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d

line 26why, one step back
26

minor revision of documentation.bagrattebefore adoption9a80fe6

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

Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89

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 #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d

line 30why, one step back
30

Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89

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

Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d

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

Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89

line 36why, one step back
36

Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d

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

Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89

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

Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d

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

Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89

line 44why, one step back
44

Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d

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