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 fileTake it
The walk stopped after 200 commits: lines older than 70de011 point at it.
Convert readthedocs link for their .org -> .io migration for hosted projects (#1827)Adam Chainzbefore adoption70de011
lines 1–5why, one step back1.. _deferred-callbacks:23Deferred Request Callbacks4==========================5Merge pull request #2279 from davidism/docs-after_this_requestDavid Lordbefore adoption716edfd
lines 6–10why, one step back6One of the design principles of Flask is that response objects are created and7passed 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.Convert readthedocs link for their .org -> .io migration for hosted projects (#1827)Adam Chainzbefore adoption70de011
line 11why, one step back11Merge pull request #2279 from davidism/docs-after_this_requestDavid Lordbefore adoption716edfd
lines 12–15why, one step back12What happens if you want to modify the response at a point where the response13does 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.Convert readthedocs link for their .org -> .io migration for hosted projects (#1827)Adam Chainzbefore adoption70de011
line 16why, one step back16Merge pull request #2279 from davidism/docs-after_this_requestDavid Lordbefore adoption716edfd
lines 17–20why, one step back17One way is to avoid the situation. Very often that is possible. For instance18you 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.Convert readthedocs link for their .org -> .io migration for hosted projects (#1827)Adam Chainzbefore adoption70de011
line 21why, one step back21Merge pull request #2279 from davidism/docs-after_this_requestDavid Lordbefore adoption716edfd
lines 22–25why, one step back22As an alternative, you can use :func:`~flask.after_this_request` to register23callbacks that will execute after only the current request. This way you can24defer code execution from anywhere in the application, based on the current25request.Convert readthedocs link for their .org -> .io migration for hosted projects (#1827)Adam Chainzbefore adoption70de011
lines 26–27why, one step back2627At any time during a request, we can register a function to be called at theMerge pull request #2279 from davidism/docs-after_this_requestDavid Lordbefore adoption716edfd
lines 28–29why, one step back28end of the request. For example you can remember the current language of the29user in a cookie in a :meth:`~flask.Flask.before_request` callback::Convert readthedocs link for their .org -> .io migration for hosted projects (#1827)Adam Chainzbefore adoption70de011
line 30why, one step back30Merge pull request #1965 from vioan/masterDavid Lordbefore adoption0420cb2
line 31why, one step back31 from flask import request, after_this_requestConvert readthedocs link for their .org -> .io migration for hosted projects (#1827)Adam Chainzbefore adoption70de011
lines 32–35why, one step back3233 @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 back36Convert readthedocs link for their .org -> .io migration for hosted projects (#1827)Adam Chainzbefore adoption70de011
lines 37–38why, one step back37 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 back3940 # when the response exists, set a cookie with the languageConvert readthedocs link for their .org -> .io migration for hosted projects (#1827)Adam Chainzbefore adoption70de011
lines 41–43why, one step back41 @after_this_request42 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 back44Convert readthedocs link for their .org -> .io migration for hosted projects (#1827)Adam Chainzbefore adoption70de011
line 45why, one step back45 g.language = language