Updated the documentation to show how to set cookies for not yet existing responses
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent 7331ae3 · Browse files · 3 files·+78 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+78 added−0 removed
- other:+78 added−0 removed
Provenance
- Commit
b40af3c- Tree
e7cc2cb- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
7331ae3
3 files changed+78 added−0 removed
added:docs/patterns/deferredcallbacks.rst+73 added
1+.. _deferred-callbacks:2+3+Deferred Request Callbacks4+==========================5+6+One of the design principles of Flask is that response objects are created7+and passed down a chain of potential callbacks that can modify them or8+replace them. When the request handling starts, there is no response9+object yet. It is created as necessary either by a view function or by10+some other component in the system.11+12+But what happens if you want to modify the response at a point where the13+response does not exist yet? A common example for that would be a14+before-request function that wants to set a cookie on the response object.15+16+One way is to avoid the situation. Very often that is possible. For17+instance you can try to move that logic into an after-request callback18+instead. Sometimes however moving that code there is just not a very19+pleasant experience or makes code look very awkward.20+21+As an alternative possibility you can attach a bunch of callback functions22+to the :data:`~flask.g` object and call then at the end of the request.23+This way you can defer code execution from anywhere in the application.24+25+26+The Decorator27+-------------28+29+The following decorator is the key. It registers a function on a list on30+the :data:`~flask.g` object::31+32+ from flask import g33+34+ def after_this_request(f):35+ if not hasattr(g, 'after_request_callbacks'):36+ g.after_request_callbacks = []37+ g.after_request_callbacks.append(f)38+ return f39+40+41+Calling the Deferred42+--------------------43+44+Now you can use the `after_this_request` decorator to mark a function to45+be called at the end of the request. But we still need to call them. For46+this the following function needs to be registered as47+:meth:`~flask.Flask.after_request` callback::48+49+ @app.after_request50+ def call_after_request_callbacks(response):51+ for callback in getattr(g, 'after_request_callbacks', ()):52+ response = callback(response)53+ return response54+55+56+A Practical Example57+-------------------58+59+Now we can easily at any point in time register a function to be called at60+the end of this particular request. For example you can remember the61+current language of the user in a cookie in the before-request function::62+63+ from flask import request64+65+ @app.before_request66+ def detect_user_language():67+ language = request.cookies.get('user_lang')68+ if language is None:69+ language = guess_language_from_request()70+ @after_this_request71+ def remember_language(response):72+ response.set_cookie('user_lang', language)73+ g.language = languagemodified:docs/patterns/index.rst+1 added
34 unmodified lines35 lazyloading36 mongokit37 favicon38 streaming39+ cookiesmodified:docs/quickstart.rst+4 added
619 unmodified lines620 just return strings from the view functions Flask will convert them into621 response objects for you. If you explicitly want to do that you can use622 the :meth:`~flask.make_response` function and then modify it.623 624+Sometimes you might want to set a cookie at a point where the response625+object does not exist yet. This is possible by utilizing the626+:ref:`deferred-callbacks` pattern.627+628 For this also see :ref:`about-responses`.629 630 Redirects and Errors631 --------------------193 unmodified lines