%PDF- %PDF-
Direktori : /usr/share/doc/python3-routes/html/modules/ |
Current File : //usr/share/doc/python3-routes/html/modules/util.html |
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" /> <title>routes.util – URL Generator and utility functions — Routes 2.5.0 documentation</title> <link rel="stylesheet" type="text/css" href="../_static/pygments.css" /> <link rel="stylesheet" type="text/css" href="../_static/classic.css" /> <script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script> <script src="../_static/jquery.js"></script> <script src="../_static/underscore.js"></script> <script src="../_static/doctools.js"></script> <link rel="index" title="Index" href="../genindex.html" /> <link rel="search" title="Search" href="../search.html" /> <link rel="prev" title="routes.middleware – Routes WSGI Middleware" href="middleware.html" /> </head><body> <div class="related" role="navigation" aria-label="related navigation"> <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="middleware.html" title="routes.middleware – Routes WSGI Middleware" accesskey="P">previous</a> |</li> <li class="nav-item nav-item-0"><a href="../index.html">Routes 2.5.0 documentation</a> »</li> <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">Routes Modules</a> »</li> <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">routes.util</span></code> – URL Generator and utility functions</a></li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body" role="main"> <section id="module-routes.util"> <span id="routes-util-url-generator-and-utility-functions"></span><h1><a class="reference internal" href="#module-routes.util" title="routes.util"><code class="xref py py-mod docutils literal notranslate"><span class="pre">routes.util</span></code></a> – URL Generator and utility functions<a class="headerlink" href="#module-routes.util" title="Permalink to this headline">¶</a></h1> <p>Utility functions for use in templates / controllers</p> <p><em>PLEASE NOTE</em>: Many of these functions expect an initialized RequestConfig object. This is expected to have been initialized for EACH REQUEST by the web framework.</p> <section id="module-contents"> <h2>Module Contents<a class="headerlink" href="#module-contents" title="Permalink to this headline">¶</a></h2> <dl class="py exception"> <dt class="sig sig-object py" id="routes.util.RoutesException"> <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">routes.util.</span></span><span class="sig-name descname"><span class="pre">RoutesException</span></span><a class="headerlink" href="#routes.util.RoutesException" title="Permalink to this definition">¶</a></dt> <dd><p>Tossed during Route exceptions</p> </dd></dl> <dl class="py exception"> <dt class="sig sig-object py" id="routes.util.MatchException"> <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">routes.util.</span></span><span class="sig-name descname"><span class="pre">MatchException</span></span><a class="headerlink" href="#routes.util.MatchException" title="Permalink to this definition">¶</a></dt> <dd><p>Tossed during URL matching exceptions</p> </dd></dl> <dl class="py exception"> <dt class="sig sig-object py" id="routes.util.GenerationException"> <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">routes.util.</span></span><span class="sig-name descname"><span class="pre">GenerationException</span></span><a class="headerlink" href="#routes.util.GenerationException" title="Permalink to this definition">¶</a></dt> <dd><p>Tossed during URL generation exceptions</p> </dd></dl> <dl class="py class"> <dt class="sig sig-object py" id="routes.util.URLGenerator"> <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">routes.util.</span></span><span class="sig-name descname"><span class="pre">URLGenerator</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">mapper</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">environ</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#routes.util.URLGenerator" title="Permalink to this definition">¶</a></dt> <dd><p>The URL Generator generates URL’s</p> <p>It is automatically instantiated by the RoutesMiddleware and put into the <code class="docutils literal notranslate"><span class="pre">wsgiorg.routing_args</span></code> tuple accessible as:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">url</span> <span class="o">=</span> <span class="n">environ</span><span class="p">[</span><span class="s1">'wsgiorg.routing_args'</span><span class="p">][</span><span class="mi">0</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span> </pre></div> </div> <p>Or via the <code class="docutils literal notranslate"><span class="pre">routes.url</span></code> key:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">url</span> <span class="o">=</span> <span class="n">environ</span><span class="p">[</span><span class="s1">'routes.url'</span><span class="p">]</span> </pre></div> </div> <p>The url object may be instantiated outside of a web context for use in testing, however sub_domain support and fully qualified URL’s cannot be generated without supplying a dict that must contain the key <code class="docutils literal notranslate"><span class="pre">HTTP_HOST</span></code>.</p> <p>Instantiate the URLGenerator</p> <dl class="simple"> <dt><code class="docutils literal notranslate"><span class="pre">mapper</span></code></dt><dd><p>The mapper object to use when generating routes.</p> </dd> <dt><code class="docutils literal notranslate"><span class="pre">environ</span></code></dt><dd><p>The environment dict used in WSGI, alternately, any dict that contains at least an <code class="docutils literal notranslate"><span class="pre">HTTP_HOST</span></code> value.</p> </dd> </dl> <dl class="py method"> <dt class="sig sig-object py" id="routes.util.URLGenerator.current"> <span class="sig-name descname"><span class="pre">current</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#routes.util.URLGenerator.current" title="Permalink to this definition">¶</a></dt> <dd><p>Generate a route that includes params used on the current request</p> <p>The arguments for this method are identical to <code class="docutils literal notranslate"><span class="pre">__call__</span></code> except that arguments set to None will remove existing route matches of the same name from the set of arguments used to construct a URL.</p> </dd></dl> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="routes.util.url_for"> <span class="sig-prename descclassname"><span class="pre">routes.util.</span></span><span class="sig-name descname"><span class="pre">url_for</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kargs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#routes.util.url_for" title="Permalink to this definition">¶</a></dt> <dd><p>Generates a URL</p> <p>All keys given to url_for are sent to the Routes Mapper instance for generation except for:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">anchor</span> <span class="n">specified</span> <span class="n">the</span> <span class="n">anchor</span> <span class="n">name</span> <span class="n">to</span> <span class="n">be</span> <span class="n">appened</span> <span class="n">to</span> <span class="n">the</span> <span class="n">path</span> <span class="n">host</span> <span class="n">overrides</span> <span class="n">the</span> <span class="n">default</span> <span class="p">(</span><span class="n">current</span><span class="p">)</span> <span class="n">host</span> <span class="k">if</span> <span class="n">provided</span> <span class="n">protocol</span> <span class="n">overrides</span> <span class="n">the</span> <span class="n">default</span> <span class="p">(</span><span class="n">current</span><span class="p">)</span> <span class="n">protocol</span> <span class="k">if</span> <span class="n">provided</span> <span class="n">qualified</span> <span class="n">creates</span> <span class="n">the</span> <span class="n">URL</span> <span class="k">with</span> <span class="n">the</span> <span class="n">host</span><span class="o">/</span><span class="n">port</span> <span class="n">information</span> <span class="k">as</span> <span class="n">needed</span> </pre></div> </div> <p>The URL is generated based on the rest of the keys. When generating a new URL, values will be used from the current request’s parameters (if present). The following rules are used to determine when and how to keep the current requests parameters:</p> <ul class="simple"> <li><p>If the controller is present and begins with ‘/’, no defaults are used</p></li> <li><p>If the controller is changed, action is set to ‘index’ unless otherwise specified</p></li> </ul> <p>For example, if the current request yielded a dict of {‘controller’: ‘blog’, ‘action’: ‘view’, ‘id’: 2}, with the standard ‘:controller/:action/:id’ route, you’d get the following results:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">url_for</span><span class="p">(</span><span class="nb">id</span><span class="o">=</span><span class="mi">4</span><span class="p">)</span> <span class="o">=></span> <span class="s1">'/blog/view/4'</span><span class="p">,</span> <span class="n">url_for</span><span class="p">(</span><span class="n">controller</span><span class="o">=</span><span class="s1">'/admin'</span><span class="p">)</span> <span class="o">=></span> <span class="s1">'/admin'</span><span class="p">,</span> <span class="n">url_for</span><span class="p">(</span><span class="n">controller</span><span class="o">=</span><span class="s1">'admin'</span><span class="p">)</span> <span class="o">=></span> <span class="s1">'/admin/view/2'</span> <span class="n">url_for</span><span class="p">(</span><span class="n">action</span><span class="o">=</span><span class="s1">'edit'</span><span class="p">)</span> <span class="o">=></span> <span class="s1">'/blog/edit/2'</span><span class="p">,</span> <span class="n">url_for</span><span class="p">(</span><span class="n">action</span><span class="o">=</span><span class="s1">'list'</span><span class="p">,</span> <span class="nb">id</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span> <span class="o">=></span> <span class="s1">'/blog/list'</span> </pre></div> </div> <p><strong>Static and Named Routes</strong></p> <p>If there is a string present as the first argument, a lookup is done against the named routes table to see if there’s any matching routes. The keyword defaults used with static routes will be sent in as GET query arg’s if a route matches.</p> <p>If no route by that name is found, the string is assumed to be a raw URL. Should the raw URL begin with <code class="docutils literal notranslate"><span class="pre">/</span></code> then appropriate SCRIPT_NAME data will be added if present, otherwise the string will be used as the url with keyword args becoming GET query args.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="routes.util._url_quote"> <span class="sig-prename descclassname"><span class="pre">routes.util.</span></span><span class="sig-name descname"><span class="pre">_url_quote</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">string</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">encoding</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#routes.util._url_quote" title="Permalink to this definition">¶</a></dt> <dd><p>A Unicode handling version of urllib.quote.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="routes.util._str_encode"> <span class="sig-prename descclassname"><span class="pre">routes.util.</span></span><span class="sig-name descname"><span class="pre">_str_encode</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">string</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">encoding</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#routes.util._str_encode" title="Permalink to this definition">¶</a></dt> <dd></dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="routes.util._screenargs"> <span class="sig-prename descclassname"><span class="pre">routes.util.</span></span><span class="sig-name descname"><span class="pre">_screenargs</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">kargs</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">mapper</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">environ</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">force_explicit</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#routes.util._screenargs" title="Permalink to this definition">¶</a></dt> <dd><p>Private function that takes a dict, and screens it against the current request dict to determine what the dict should look like that is used. This is responsible for the requests “memory” of the current.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="routes.util._subdomain_check"> <span class="sig-prename descclassname"><span class="pre">routes.util.</span></span><span class="sig-name descname"><span class="pre">_subdomain_check</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">kargs</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">mapper</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">environ</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#routes.util._subdomain_check" title="Permalink to this definition">¶</a></dt> <dd><p>Screen the kargs for a subdomain and alter it appropriately depending on the current subdomain or lack therof.</p> </dd></dl> </section> </section> <div class="clearer"></div> </div> </div> </div> <div class="sphinxsidebar" role="navigation" aria-label="main navigation"> <div class="sphinxsidebarwrapper"> <h3><a href="../index.html">Table of Contents</a></h3> <ul> <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">routes.util</span></code> – URL Generator and utility functions</a><ul> <li><a class="reference internal" href="#module-contents">Module Contents</a></li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="middleware.html" title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">routes.middleware</span></code> – Routes WSGI Middleware</a></p> <div role="note" aria-label="source link"> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../_sources/modules/util.rst.txt" rel="nofollow">Show Source</a></li> </ul> </div> <div 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> </div> <script>$('#searchbox').show(0);</script> </div> </div> <div class="clearer"></div> </div> <div class="related" role="navigation" aria-label="related navigation"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="middleware.html" title="routes.middleware – Routes WSGI Middleware" >previous</a> |</li> <li class="nav-item nav-item-0"><a href="../index.html">Routes 2.5.0 documentation</a> »</li> <li class="nav-item nav-item-1"><a href="index.html" >Routes Modules</a> »</li> <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">routes.util</span></code> – URL Generator and utility functions</a></li> </ul> </div> <div class="footer" role="contentinfo"> © Copyright 2005-2022, Ben Bangert, Mike Orr, and numerous contributers. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.3.2. </div> </body> </html>