134 lines
No EOL
7.9 KiB
HTML
134 lines
No EOL
7.9 KiB
HTML
<!DOCTYPE html>
|
|
|
|
<html lang="en" data-content_root="../">
|
|
<head>
|
|
<meta charset="utf-8" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
<title>Deferred Request Callbacks — Flask Documentation (3.2.x)</title>
|
|
<link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=6625fa76" />
|
|
<link rel="stylesheet" type="text/css" href="../_static/flask.css?v=b87c8d14" />
|
|
<script src="../_static/documentation_options.js?v=56528222"></script>
|
|
<script src="../_static/doctools.js?v=9bcbadda"></script>
|
|
<script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
|
|
<script data-project="flask" data-version="3.2.x" src="../_static/describe_version.js?v=fa7f30d0"></script>
|
|
<link rel="icon" href="../_static/shortcut-icon.png"/>
|
|
<link rel="index" title="Index" href="../genindex.html" />
|
|
<link rel="search" title="Search" href="../search.html" />
|
|
<link rel="next" title="Adding HTTP Method Overrides" href="methodoverrides.html" />
|
|
<link rel="prev" title="Streaming Contents" href="streaming.html" />
|
|
</head><body>
|
|
<div class="related" role="navigation" aria-label="Related">
|
|
<h3>Navigation</h3>
|
|
<ul>
|
|
<li class="right" style="margin-right: 10px">
|
|
<a href="../genindex.html" title="General Index"
|
|
accesskey="I">index</a></li>
|
|
<li class="right" >
|
|
<a href="../py-modindex.html" title="Python Module Index"
|
|
>modules</a> |</li>
|
|
<li class="right" >
|
|
<a href="methodoverrides.html" title="Adding HTTP Method Overrides"
|
|
accesskey="N">next</a> |</li>
|
|
<li class="right" >
|
|
<a href="streaming.html" title="Streaming Contents"
|
|
accesskey="P">previous</a> |</li>
|
|
<li class="nav-item nav-item-0"><a href="../index.html">Flask Documentation (3.2.x)</a> »</li>
|
|
<li class="nav-item nav-item-1"><a href="index.html" accesskey="U">Patterns for Flask</a> »</li>
|
|
<li class="nav-item nav-item-this"><a href="">Deferred Request Callbacks</a></li>
|
|
</ul>
|
|
</div>
|
|
|
|
<div class="document">
|
|
<div class="documentwrapper">
|
|
<div class="bodywrapper">
|
|
<div class="body" role="main">
|
|
|
|
<section id="deferred-request-callbacks">
|
|
<h1>Deferred Request Callbacks<a class="headerlink" href="#deferred-request-callbacks" title="Link to this heading">¶</a></h1>
|
|
<p>One of the design principles of Flask is that response objects are created and
|
|
passed down a chain of potential callbacks that can modify them or replace
|
|
them. When the request handling starts, there is no response object yet. It is
|
|
created as necessary either by a view function or by some other component in
|
|
the system.</p>
|
|
<p>What happens if you want to modify the response at a point where the response
|
|
does not exist yet? A common example for that would be a
|
|
<a class="reference internal" href="../api.html#flask.Flask.before_request" title="flask.Flask.before_request"><code class="xref py py-meth docutils literal notranslate"><span class="pre">before_request()</span></code></a> callback that wants to set a cookie on the
|
|
response object.</p>
|
|
<p>One way is to avoid the situation. Very often that is possible. For instance
|
|
you can try to move that logic into a <a class="reference internal" href="../api.html#flask.Flask.after_request" title="flask.Flask.after_request"><code class="xref py py-meth docutils literal notranslate"><span class="pre">after_request()</span></code></a>
|
|
callback instead. However, sometimes moving code there makes it
|
|
more complicated or awkward to reason about.</p>
|
|
<p>As an alternative, you can use <a class="reference internal" href="../api.html#flask.after_this_request" title="flask.after_this_request"><code class="xref py py-func docutils literal notranslate"><span class="pre">after_this_request()</span></code></a> to register
|
|
callbacks that will execute after only the current request. This way you can
|
|
defer code execution from anywhere in the application, based on the current
|
|
request.</p>
|
|
<p>At any time during a request, we can register a function to be called at the
|
|
end of the request. For example you can remember the current language of the
|
|
user in a cookie in a <a class="reference internal" href="../api.html#flask.Flask.before_request" title="flask.Flask.before_request"><code class="xref py py-meth docutils literal notranslate"><span class="pre">before_request()</span></code></a> callback:</p>
|
|
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span><span class="w"> </span><span class="nn">flask</span><span class="w"> </span><span class="kn">import</span> <span class="n">request</span><span class="p">,</span> <span class="n">after_this_request</span>
|
|
|
|
<span class="nd">@app</span><span class="o">.</span><span class="n">before_request</span>
|
|
<span class="k">def</span><span class="w"> </span><span class="nf">detect_user_language</span><span class="p">():</span>
|
|
<span class="n">language</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">cookies</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'user_lang'</span><span class="p">)</span>
|
|
|
|
<span class="k">if</span> <span class="n">language</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
|
|
<span class="n">language</span> <span class="o">=</span> <span class="n">guess_language_from_request</span><span class="p">()</span>
|
|
|
|
<span class="c1"># when the response exists, set a cookie with the language</span>
|
|
<span class="nd">@after_this_request</span>
|
|
<span class="k">def</span><span class="w"> </span><span class="nf">remember_language</span><span class="p">(</span><span class="n">response</span><span class="p">):</span>
|
|
<span class="n">response</span><span class="o">.</span><span class="n">set_cookie</span><span class="p">(</span><span class="s1">'user_lang'</span><span class="p">,</span> <span class="n">language</span><span class="p">)</span>
|
|
<span class="k">return</span> <span class="n">response</span>
|
|
|
|
<span class="n">g</span><span class="o">.</span><span class="n">language</span> <span class="o">=</span> <span class="n">language</span>
|
|
</pre></div>
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<div class="clearer"></div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<span id="sidebar-top"></span>
|
|
<div class="sphinxsidebar" role="navigation" aria-label="Main">
|
|
<div class="sphinxsidebarwrapper">
|
|
|
|
|
|
<p class="logo"><a href="../index.html">
|
|
<img class="logo" src="../_static/flask-vertical.png" alt="Logo of Flask"/>
|
|
</a></p>
|
|
|
|
<h3>Navigation</h3>
|
|
<ul>
|
|
<li><a href="../index.html">Overview</a>
|
|
<ul>
|
|
<li><a href="index.html">Patterns for Flask</a>
|
|
<ul>
|
|
<li>Previous: <a href="streaming.html" title="previous chapter">Streaming Contents</a>
|
|
<li>Next: <a href="methodoverrides.html" title="next chapter">Adding HTTP Method Overrides</a></ul>
|
|
</li>
|
|
</ul>
|
|
</li>
|
|
</ul>
|
|
<search id="searchbox" style="display: none" role="search">
|
|
<h3 id="searchlabel">Quick search</h3>
|
|
<div class="searchformwrapper">
|
|
<form class="search" action="../search.html" method="get">
|
|
<input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
|
|
<input type="submit" value="Go" />
|
|
</form>
|
|
</div>
|
|
</search>
|
|
<script>document.getElementById('searchbox').style.display = "block"</script><div id="ethical-ad-placement"></div>
|
|
</div>
|
|
</div>
|
|
<div class="clearer"></div>
|
|
</div>
|
|
<div class="footer" role="contentinfo">
|
|
© Copyright 2010 Pallets.
|
|
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 8.1.3.
|
|
</div>
|
|
</body>
|
|
</html> |