Skip to content
Myr

deferredcallbacks.rst

This file

Path

docs/patterns/deferredcallbacks.rst

45 lines · 1.8 KiB

Last changed by

resolve merge conflictsKenneth Reitzbefore adoption31174fe

History of this file

Take it

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

Merge pull request #1185 from defuz/patch-2Markus Unterwaditzerbefore adoption7550f43

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

resolve merge conflictsKenneth Reitzbefore adoption31174fe

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 #1185 from defuz/patch-2Markus Unterwaditzerbefore adoption7550f43

line 11why, one step back
11

resolve merge conflictsKenneth Reitzbefore adoption31174fe

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 #1185 from defuz/patch-2Markus Unterwaditzerbefore adoption7550f43

line 16why, one step back
16

resolve merge conflictsKenneth Reitzbefore adoption31174fe

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 #1185 from defuz/patch-2Markus Unterwaditzerbefore adoption7550f43

line 21why, one step back
21

resolve merge conflictsKenneth Reitzbefore adoption31174fe

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 #1185 from defuz/patch-2Markus Unterwaditzerbefore adoption7550f43

line 26why, one step back
26

resolve merge conflictsKenneth Reitzbefore adoption31174fe

lines 27–29why, one step back
27At any time during a request, we can register a function to be called at the
28end of the request. For example you can remember the current language of the29user in a cookie in a :meth:`~flask.Flask.before_request` callback::

Merge pull request #1185 from defuz/patch-2Markus Unterwaditzerbefore adoption7550f43

line 30why, one step back
30

resolve merge conflictsKenneth Reitzbefore adoption31174fe

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

Merge pull request #1185 from defuz/patch-2Markus Unterwaditzerbefore adoption7550f43

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

resolve merge conflictsKenneth Reitzbefore adoption31174fe

line 36why, one step back
36

Merge pull request #1185 from defuz/patch-2Markus Unterwaditzerbefore adoption7550f43

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

resolve merge conflictsKenneth Reitzbefore adoption31174fe

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

Merge pull request #1185 from defuz/patch-2Markus Unterwaditzerbefore adoption7550f43

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

resolve merge conflictsKenneth Reitzbefore adoption31174fe

line 44why, one step back
44

Merge pull request #1185 from defuz/patch-2Markus Unterwaditzerbefore adoption7550f43

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