Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/app/static/doc/flask-docs/patterns/urlprocessors.html
diff options
context:
space:
mode:
Diffstat (limited to 'app/static/doc/flask-docs/patterns/urlprocessors.html')
-rw-r--r--app/static/doc/flask-docs/patterns/urlprocessors.html237
1 files changed, 0 insertions, 237 deletions
diff --git a/app/static/doc/flask-docs/patterns/urlprocessors.html b/app/static/doc/flask-docs/patterns/urlprocessors.html
deleted file mode 100644
index 8e50e36..0000000
--- a/app/static/doc/flask-docs/patterns/urlprocessors.html
+++ /dev/null
@@ -1,237 +0,0 @@
-
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
-
-<html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
-
- <title>Using URL Processors &mdash; Flask 0.8 documentation</title>
-
- <link rel="stylesheet" href="../_static/flasky.css" type="text/css" />
- <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
-
- <script type="text/javascript">
- var DOCUMENTATION_OPTIONS = {
- URL_ROOT: '../',
- VERSION: '0.8',
- COLLAPSE_INDEX: false,
- FILE_SUFFIX: '.html',
- HAS_SOURCE: true
- };
- </script>
- <script type="text/javascript" src="../_static/jquery.js"></script>
- <script type="text/javascript" src="../_static/underscore.js"></script>
- <script type="text/javascript" src="../_static/doctools.js"></script>
- <link rel="top" title="Flask 0.8 documentation" href="../index.html" />
- <link rel="up" title="Patterns for Flask" href="index.html" />
- <link rel="next" title="Deploying with Distribute" href="distribute.html" />
- <link rel="prev" title="Application Dispatching" href="appdispatch.html" />
-
-
- <link rel="apple-touch-icon" href="../_static/touch-icon.png" />
-
- <link media="only screen and (max-device-width: 480px)" href="../_static/small_flask.css" type= "text/css" rel="stylesheet" />
-
- </head>
- <body>
- <div class="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="distribute.html" title="Deploying with Distribute"
- accesskey="N">next</a> |</li>
- <li class="right" >
- <a href="appdispatch.html" title="Application Dispatching"
- accesskey="P">previous</a> |</li>
- <li><a href="../index.html">Flask 0.8 documentation</a> &raquo;</li>
- <li><a href="index.html" accesskey="U">Patterns for Flask</a> &raquo;</li>
- </ul>
- </div>
-
- <div class="document">
- <div class="documentwrapper">
- <div class="bodywrapper">
- <div class="body">
-
- <div class="section" id="using-url-processors">
-<h1>Using URL Processors<a class="headerlink" href="#using-url-processors" title="Permalink to this headline">¶</a></h1>
-<p class="versionadded">
-<span class="versionmodified">New in version 0.7.</span></p>
-<p>Flask 0.7 introduces the concept of URL processors. The idea is that you
-might have a bunch of resources with common parts in the URL that you
-don&#8217;t always explicitly want to provide. For instance you might have a
-bunch of URLs that have the language code in it but you don&#8217;t want to have
-to handle it in every single function yourself.</p>
-<p>URL processors are especially helpful when combined with blueprints. We
-will handle both application specific URL processors here as well as
-blueprint specifics.</p>
-<div class="section" id="internationalized-application-urls">
-<h2>Internationalized Application URLs<a class="headerlink" href="#internationalized-application-urls" title="Permalink to this headline">¶</a></h2>
-<p>Consider an application like this:</p>
-<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">flask</span> <span class="kn">import</span> <span class="n">Flask</span><span class="p">,</span> <span class="n">g</span>
-
-<span class="n">app</span> <span class="o">=</span> <span class="n">Flask</span><span class="p">(</span><span class="n">__name__</span><span class="p">)</span>
-
-<span class="nd">@app.route</span><span class="p">(</span><span class="s">&#39;/&lt;lang_code&gt;/&#39;</span><span class="p">)</span>
-<span class="k">def</span> <span class="nf">index</span><span class="p">(</span><span class="n">lang_code</span><span class="p">):</span>
- <span class="n">g</span><span class="o">.</span><span class="n">lang_code</span> <span class="o">=</span> <span class="n">lang_code</span>
- <span class="o">...</span>
-
-<span class="nd">@app.route</span><span class="p">(</span><span class="s">&#39;/&lt;lang_code&gt;/about&#39;</span><span class="p">)</span>
-<span class="k">def</span> <span class="nf">about</span><span class="p">(</span><span class="n">lang_code</span><span class="p">):</span>
- <span class="n">g</span><span class="o">.</span><span class="n">lang_code</span> <span class="o">=</span> <span class="n">lang_code</span>
- <span class="o">...</span>
-</pre></div>
-</div>
-<p>This is an awful lot of repetition as you have to handle the language code
-setting on the <a class="reference internal" href="../api.html#flask.g" title="flask.g"><tt class="xref py py-data docutils literal"><span class="pre">g</span></tt></a> object yourself in every single function.
-Sure, a decorator could be used to simplify this, but if you want to
-generate URLs from one function to another you would have to still provide
-the language code explicitly which can be annoying.</p>
-<p>For the latter, this is where <a class="reference internal" href="../api.html#flask.Flask.url_defaults" title="flask.Flask.url_defaults"><tt class="xref py py-func docutils literal"><span class="pre">url_defaults()</span></tt></a> functions
-come in. They can automatically inject values into a call for
-<a class="reference internal" href="../api.html#flask.url_for" title="flask.url_for"><tt class="xref py py-func docutils literal"><span class="pre">url_for()</span></tt></a> automatically. The code below checks if the
-language code is not yet in the dictionary of URL values and if the
-endpoint wants a value named <tt class="docutils literal"><span class="pre">'lang_code'</span></tt>:</p>
-<div class="highlight-python"><div class="highlight"><pre><span class="nd">@app.url_defaults</span>
-<span class="k">def</span> <span class="nf">add_language_code</span><span class="p">(</span><span class="n">endpoint</span><span class="p">,</span> <span class="n">values</span><span class="p">):</span>
- <span class="k">if</span> <span class="s">&#39;lang_code&#39;</span> <span class="ow">in</span> <span class="n">values</span> <span class="ow">or</span> <span class="ow">not</span> <span class="n">g</span><span class="o">.</span><span class="n">lang_code</span><span class="p">:</span>
- <span class="k">return</span>
- <span class="k">if</span> <span class="n">app</span><span class="o">.</span><span class="n">url_map</span><span class="o">.</span><span class="n">is_endpoint_expecting</span><span class="p">(</span><span class="n">endpoint</span><span class="p">,</span> <span class="s">&#39;lang_code&#39;</span><span class="p">):</span>
- <span class="n">values</span><span class="p">[</span><span class="s">&#39;lang_code&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">g</span><span class="o">.</span><span class="n">lang_code</span>
-</pre></div>
-</div>
-<p>The method <a class="reference external" href="http://werkzeug.pocoo.org/docs/routing/#werkzeug.routing.Map.is_endpoint_expecting" title="(in Werkzeug v0.7)"><tt class="xref py py-meth docutils literal"><span class="pre">is_endpoint_expecting()</span></tt></a> of the URL
-map can be used to figure out if it would make sense to provide a language
-code for the given endpoint.</p>
-<p>The reverse of that function are
-<a class="reference internal" href="../api.html#flask.Flask.url_value_preprocessor" title="flask.Flask.url_value_preprocessor"><tt class="xref py py-meth docutils literal"><span class="pre">url_value_preprocessor()</span></tt></a>s. They are executed right
-after the request was matched and can execute code based on the URL
-values. The idea is that they pull information out of the values
-dictionary and put it somewhere else:</p>
-<div class="highlight-python"><div class="highlight"><pre><span class="nd">@app.url_value_preprocessor</span>
-<span class="k">def</span> <span class="nf">pull_lang_code</span><span class="p">(</span><span class="n">endpoint</span><span class="p">,</span> <span class="n">values</span><span class="p">):</span>
- <span class="n">g</span><span class="o">.</span><span class="n">lang_code</span> <span class="o">=</span> <span class="n">values</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s">&#39;lang_code&#39;</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span>
-</pre></div>
-</div>
-<p>That way you no longer have to do the <cite>lang_code</cite> assigment to
-<a class="reference internal" href="../api.html#flask.g" title="flask.g"><tt class="xref py py-data docutils literal"><span class="pre">g</span></tt></a> in every function. You can further improve that by
-writing your own decorator that prefixes URLs with the language code, but
-the more beautiful solution is using a blueprint. Once the
-<tt class="docutils literal"><span class="pre">'lang_code'</span></tt> is popped from the values dictionary and it will no longer
-be forwarded to the view function reducing the code to this:</p>
-<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">flask</span> <span class="kn">import</span> <span class="n">Flask</span><span class="p">,</span> <span class="n">g</span>
-
-<span class="n">app</span> <span class="o">=</span> <span class="n">Flask</span><span class="p">(</span><span class="n">__name__</span><span class="p">)</span>
-
-<span class="nd">@app.url_defaults</span>
-<span class="k">def</span> <span class="nf">add_language_code</span><span class="p">(</span><span class="n">endpoint</span><span class="p">,</span> <span class="n">values</span><span class="p">):</span>
- <span class="k">if</span> <span class="s">&#39;lang_code&#39;</span> <span class="ow">in</span> <span class="n">values</span> <span class="ow">or</span> <span class="ow">not</span> <span class="n">g</span><span class="o">.</span><span class="n">lang_code</span><span class="p">:</span>
- <span class="k">return</span>
- <span class="k">if</span> <span class="n">app</span><span class="o">.</span><span class="n">url_map</span><span class="o">.</span><span class="n">is_endpoint_expecting</span><span class="p">(</span><span class="n">endpoint</span><span class="p">,</span> <span class="s">&#39;lang_code&#39;</span><span class="p">):</span>
- <span class="n">values</span><span class="p">[</span><span class="s">&#39;lang_code&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">g</span><span class="o">.</span><span class="n">lang_code</span>
-
-<span class="nd">@app.url_value_preprocessor</span>
-<span class="k">def</span> <span class="nf">pull_lang_code</span><span class="p">(</span><span class="n">endpoint</span><span class="p">,</span> <span class="n">values</span><span class="p">):</span>
- <span class="n">g</span><span class="o">.</span><span class="n">lang_code</span> <span class="o">=</span> <span class="n">values</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s">&#39;lang_code&#39;</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span>
-
-<span class="nd">@app.route</span><span class="p">(</span><span class="s">&#39;/&lt;lang_code&gt;/&#39;</span><span class="p">)</span>
-<span class="k">def</span> <span class="nf">index</span><span class="p">():</span>
- <span class="o">...</span>
-
-<span class="nd">@app.route</span><span class="p">(</span><span class="s">&#39;/&lt;lang_code&gt;/about&#39;</span><span class="p">)</span>
-<span class="k">def</span> <span class="nf">about</span><span class="p">():</span>
- <span class="o">...</span>
-</pre></div>
-</div>
-</div>
-<div class="section" id="internationalized-blueprint-urls">
-<h2>Internationalized Blueprint URLs<a class="headerlink" href="#internationalized-blueprint-urls" title="Permalink to this headline">¶</a></h2>
-<p>Because blueprints can automatically prefix all URLs with a common string
-it&#8217;s easy to automatically do that for every function. Furthermore
-blueprints can have per-blueprint URL processors which removes a whole lot
-of logic from the <a class="reference internal" href="../api.html#flask.Flask.url_defaults" title="flask.Flask.url_defaults"><tt class="xref py py-meth docutils literal"><span class="pre">url_defaults()</span></tt></a> function because it no
-longer has to check if the URL is really interested in a <tt class="docutils literal"><span class="pre">'lang_code'</span></tt>
-parameter:</p>
-<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">flask</span> <span class="kn">import</span> <span class="n">Blueprint</span><span class="p">,</span> <span class="n">g</span>
-
-<span class="n">bp</span> <span class="o">=</span> <span class="n">Blueprint</span><span class="p">(</span><span class="s">&#39;frontend&#39;</span><span class="p">,</span> <span class="n">__name__</span><span class="p">,</span> <span class="n">url_prefix</span><span class="o">=</span><span class="s">&#39;/&lt;lang_code&gt;&#39;</span><span class="p">)</span>
-
-<span class="nd">@bp.url_defaults</span>
-<span class="k">def</span> <span class="nf">add_language_code</span><span class="p">(</span><span class="n">endpoint</span><span class="p">,</span> <span class="n">values</span><span class="p">):</span>
- <span class="n">values</span><span class="o">.</span><span class="n">setdefault</span><span class="p">(</span><span class="s">&#39;lang_code&#39;</span><span class="p">,</span> <span class="n">g</span><span class="o">.</span><span class="n">lang_code</span><span class="p">)</span>
-
-<span class="nd">@bp.url_value_preprocessor</span>
-<span class="k">def</span> <span class="nf">pull_lang_code</span><span class="p">(</span><span class="n">endpoint</span><span class="p">,</span> <span class="n">values</span><span class="p">):</span>
- <span class="n">g</span><span class="o">.</span><span class="n">lang_code</span> <span class="o">=</span> <span class="n">values</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s">&#39;lang_code&#39;</span><span class="p">)</span>
-
-<span class="nd">@bp.route</span><span class="p">(</span><span class="s">&#39;/&#39;</span><span class="p">)</span>
-<span class="k">def</span> <span class="nf">index</span><span class="p">():</span>
- <span class="o">...</span>
-
-<span class="nd">@bp.route</span><span class="p">(</span><span class="s">&#39;/about&#39;</span><span class="p">)</span>
-<span class="k">def</span> <span class="nf">about</span><span class="p">():</span>
- <span class="o">...</span>
-</pre></div>
-</div>
-</div>
-</div>
-
-
- </div>
- </div>
- </div>
- <div class="sphinxsidebar">
- <div class="sphinxsidebarwrapper"><p class="logo"><a href="../index.html">
- <img class="logo" src="../_static/flask.png" alt="Logo"/>
-</a></p>
- <h3><a href="../index.html">Table Of Contents</a></h3>
- <ul>
-<li><a class="reference internal" href="#">Using URL Processors</a><ul>
-<li><a class="reference internal" href="#internationalized-application-urls">Internationalized Application URLs</a></li>
-<li><a class="reference internal" href="#internationalized-blueprint-urls">Internationalized Blueprint URLs</a></li>
-</ul>
-</li>
-</ul>
-<h3>Related Topics</h3>
-<ul>
- <li><a href="../index.html">Documentation overview</a><ul>
- <li><a href="index.html">Patterns for Flask</a><ul>
- <li>Previous: <a href="appdispatch.html" title="previous chapter">Application Dispatching</a></li>
- <li>Next: <a href="distribute.html" title="next chapter">Deploying with Distribute</a></li>
- </ul></li>
- </ul></li>
-</ul>
- <h3>This Page</h3>
- <ul class="this-page-menu">
- <li><a href="../_sources/patterns/urlprocessors.txt"
- rel="nofollow">Show Source</a></li>
- </ul>
-<div id="searchbox" style="display: none">
- <h3>Quick search</h3>
- <form class="search" action="../search.html" method="get">
- <input type="text" name="q" />
- <input type="submit" value="Go" />
- <input type="hidden" name="check_keywords" value="yes" />
- <input type="hidden" name="area" value="default" />
- </form>
- <p class="searchtip" style="font-size: 90%">
- Enter search terms or a module, class or function name.
- </p>
-</div>
-<script type="text/javascript">$('#searchbox').show(0);</script>
- </div>
- </div>
- <div class="clearer"></div>
- </div>
- <div class="footer">
- &copy; Copyright 2010, Armin Ronacher.
- Created using <a href="http://sphinx.pocoo.org/">Sphinx</a>.
- </div>
- </body>
-</html> \ No newline at end of file