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 fileTake it
The walk stopped after 200 commits: lines older than 817b72d point at it.
Removed deprecated module functionalityArmin Ronacherbefore adoption817b72d
lines 1–5why, one step back1.. _deferred-callbacks:23Deferred Request Callbacks4==========================5resolve merge conflictsKenneth Reitzbefore adoption31174fe
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.Removed deprecated module functionalityArmin Ronacherbefore adoption817b72d
line 11why, one step back11resolve merge conflictsKenneth Reitzbefore adoption31174fe
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.Removed deprecated module functionalityArmin Ronacherbefore adoption817b72d
line 16why, one step back16resolve merge conflictsKenneth Reitzbefore adoption31174fe
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.Removed deprecated module functionalityArmin Ronacherbefore adoption817b72d
line 21why, one step back21resolve merge conflictsKenneth Reitzbefore adoption31174fe
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.Removed deprecated module functionalityArmin Ronacherbefore adoption817b72d
line 26why, one step back26resolve merge conflictsKenneth Reitzbefore adoption31174fe
lines 27–29why, one step back27At any time during a request, we can register a function to be called at the28end of the request. For example you can remember the current language of the29user in a cookie in a :meth:`~flask.Flask.before_request` callback::Removed deprecated module functionalityArmin Ronacherbefore adoption817b72d
line 30why, one step back30resolve merge conflictsKenneth Reitzbefore adoption31174fe
line 31why, one step back31 from flask import request, after_this_requestRemoved deprecated module functionalityArmin Ronacherbefore adoption817b72d
lines 32–35why, one step back3233 @app.before_request34 def detect_user_language():35 language = request.cookies.get('user_lang')resolve merge conflictsKenneth Reitzbefore adoption31174fe
line 36why, one step back36Removed deprecated module functionalityArmin Ronacherbefore adoption817b72d
lines 37–38why, one step back37 if language is None:38 language = guess_language_from_request()resolve merge conflictsKenneth Reitzbefore adoption31174fe
lines 39–40why, one step back3940 # when the response exists, set a cookie with the languageRemoved deprecated module functionalityArmin Ronacherbefore adoption817b72d
lines 41–43why, one step back41 @after_this_request42 def remember_language(response):43 response.set_cookie('user_lang', language)resolve merge conflictsKenneth Reitzbefore adoption31174fe
line 44why, one step back44Removed deprecated module functionalityArmin Ronacherbefore adoption817b72d
line 45why, one step back45 g.language = language