Skip to content
Myr

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

3 files+78 added−0 removed

added:docs/patterns/deferredcallbacks.rst+73 added
1.. _deferred-callbacks:23Deferred Request Callbacks4==========================56One of the design principles of Flask is that response objects are created7and passed down a chain of potential callbacks that can modify them or8replace them. When the request handling starts, there is no response9object yet. It is created as necessary either by a view function or by10some other component in the system.1112But what happens if you want to modify the response at a point where the13response does not exist yet? A common example for that would be a14before-request function that wants to set a cookie on the response object.1516One way is to avoid the situation. Very often that is possible. For17instance you can try to move that logic into an after-request callback18instead. Sometimes however moving that code there is just not a very19pleasant experience or makes code look very awkward.2021As an alternative possibility you can attach a bunch of callback functions22to the :data:`~flask.g` object and call then at the end of the request.23This way you can defer code execution from anywhere in the application.242526The Decorator27-------------2829The following decorator is the key. It registers a function on a list on30the :data:`~flask.g` object::3132 from flask import g3334 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 f394041Calling the Deferred42--------------------4344Now you can use the `after_this_request` decorator to mark a function to45be called at the end of the request. But we still need to call them. For46this the following function needs to be registered as47:meth:`~flask.Flask.after_request` callback::4849 @app.after_request50 def call_after_request_callbacks(response):51 for callback in getattr(g, 'after_request_callbacks', ()):52 response = callback(response)53 return response545556A Practical Example57-------------------5859Now we can easily at any point in time register a function to be called at60the end of this particular request. For example you can remember the61current language of the user in a cookie in the before-request function::6263 from flask import request6465 @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 = language
modified:docs/patterns/index.rst+1 added
34 unmodified lines
35 lazyloading36 mongokit37 favicon38 streaming39 cookies
modified:docs/quickstart.rst+4 added
619 unmodified lines
620just return strings from the view functions Flask will convert them into621response objects for you. If you explicitly want to do that you can use622the :meth:`~flask.make_response` function and then modify it.623624Sometimes you might want to set a cookie at a point where the response625object does not exist yet. This is possible by utilizing the626:ref:`deferred-callbacks` pattern.627628For this also see :ref:`about-responses`.629630Redirects and Errors631--------------------
193 unmodified lines