<!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>Unicode, Redirects, and More — Routes v1.12 documentation</title> <link rel="stylesheet" href="_static/default.css" type="text/css" /> <link rel="stylesheet" href="_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '#', VERSION: '1.12', COLLAPSE_MODINDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </script> <script type="text/javascript" src="_static/jquery.js"></script> <script type="text/javascript" src="_static/doctools.js"></script> <link rel="search" type="application/opensearchdescription+xml" title="Search within Routes v1.12 documentation" href="_static/opensearch.xml"/> <link rel="top" title="Routes v1.12 documentation" href="index.html" /> <link rel="next" title="Glossary" href="glossary.html" /> <link rel="prev" title="RESTful services" href="restful.html" /> </head> <body> <div style="color: #D1361B; font-size: 70px; font-weight: bold; padding: 10px 0 0 10px;">Routes</div> </div> <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="modindex.html" title="Global Module Index" accesskey="M">modules</a> |</li> <li class="right" > <a href="glossary.html" title="Glossary" accesskey="N">next</a> |</li> <li class="right" > <a href="restful.html" title="RESTful services" accesskey="P">previous</a> |</li> <li><a href="index.html">Routes home</a> | </li> <li><a href="contents.html">Documentation</a>»</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="unicode-redirects-and-more"> <h1>Unicode, Redirects, and More<a class="headerlink" href="#unicode-redirects-and-more" title="Permalink to this headline">¶</a></h1> <div class="section" id="unicode"> <h2>Unicode<a class="headerlink" href="#unicode" title="Permalink to this headline">¶</a></h2> <p>Routes assumes UTF-8 encoding on incoming URLs, and <tt class="docutils literal"><span class="pre">url</span></tt> and <tt class="docutils literal"><span class="pre">url_for</span></tt> also generate UTF-8. You can change the encoding with the <tt class="docutils literal"><span class="pre">map.charset</span></tt> attribute:</p> <div class="highlight-python"><div class="highlight"><pre><span class="nb">map</span><span class="o">.</span><span class="n">charset</span> <span class="o">=</span> <span class="s">"latin-1"</span> </pre></div> </div> <p>New in Routes 1.10: several bugfixes.</p> </div> <div class="section" id="redirect-routes"> <h2>Redirect Routes<a class="headerlink" href="#redirect-routes" title="Permalink to this headline">¶</a></h2> <p>Redirect routes allow you to specify redirects in the route map, similar to RewriteRule in an Apache configuration. This avoids the need to define dummy controller actions just to handle redirects. It’s especially useful when the URL structure changes and you want to redirect legacy URLs to their new equivalents. The redirection is done by the Routes middleware, and the WSGI application is not called.</p> <p><tt class="docutils literal"><span class="pre">map.redirect</span></tt> takes two positional arguments: the route path and the destination URL. Redirect routes do not have a name. Both paths can contain variables, and the route path can take inline requirements. Keyword arguments are the same as <tt class="docutils literal"><span class="pre">map.connect</span></tt>, both in regards to extra variables and to route options.</p> <div class="highlight-python"><div class="highlight"><pre><span class="nb">map</span><span class="o">.</span><span class="n">redirect</span><span class="p">(</span><span class="s">"/legacyapp/archives/{url:.*}"</span><span class="p">,</span> <span class="s">"/archives/{url}"</span><span class="p">)</span> <span class="nb">map</span><span class="o">.</span><span class="n">redirect</span><span class="p">(</span><span class="s">"/legacyapp/archives/{url:.*}"</span><span class="p">,</span> <span class="s">"/archives/{url}"</span><span class="p">)</span> </pre></div> </div> <p>By default a “302 Found” HTTP status is issued. You can override this with the <tt class="docutils literal"><span class="pre">_redirect_code</span></tt> keyword argument. The value must be an entire status string.</p> <div class="highlight-python"><div class="highlight"><pre><span class="nb">map</span><span class="o">.</span><span class="n">redirect</span><span class="p">(</span><span class="s">"/home/index"</span><span class="p">,</span> <span class="s">"/"</span><span class="p">,</span> <span class="n">_redirect_code</span><span class="o">=</span><span class="s">"301 Moved Permanently"</span><span class="p">)</span> </pre></div> </div> <p><em>New in Routes 1.10.</em></p> </div> <div class="section" id="printing"> <h2>Printing<a class="headerlink" href="#printing" title="Permalink to this headline">¶</a></h2> <p>Mappers now have a formatted string representation. In your python shell, simply print your application’s mapper:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="nb">map</span><span class="o">.</span><span class="n">collection</span><span class="p">(</span><span class="s">"entries"</span><span class="p">,</span> <span class="s">"entry"</span><span class="p">)</span> <span class="gp">>>> </span><span class="k">print</span> <span class="nb">map</span> <span class="go">Route name Methods Path</span> <span class="go">entries GET /entries{.format}</span> <span class="go">create_entry POST /entries{.format}</span> <span class="go">new_entry GET /entries/new{.format}</span> <span class="go">entry GET /entries/{id}{.format}</span> <span class="go">update_entry PUT /entries/{id}{.format}</span> <span class="go">delete_entry DELETE /entries/{id}{.format}</span> <span class="go">edit_entry GET /entries/{id}/edit{.format}</span> </pre></div> </div> <p><em>New in Routes 1.12.</em></p> </div> <div class="section" id="introspection"> <h2>Introspection<a class="headerlink" href="#introspection" title="Permalink to this headline">¶</a></h2> <p>The mapper attribute <tt class="docutils literal"><span class="pre">.matchlist</span></tt> contains the list of routes to be matched against incoming URLs. You can iterate this list to see what routes are defined. This can be useful when debugging route configurations.</p> </div> <div class="section" id="other"> <h2>Other<a class="headerlink" href="#other" title="Permalink to this headline">¶</a></h2> <p>If your application is behind an HTTP proxy such a load balancer on another host, the WSGI environment will refer to the internal server rather than to the proxy, which will mess up generated URLs. Use the ProxyMiddleware in PasteDeploy to fix the WSGI environment to what it would have been without the proxy.</p> <p>To debug routes, turn on debug logging for the “routes.middleware” logger. (See Python’s <tt class="docutils literal"><span class="pre">logging</span></tt> module to set up your logging configuration.)</p> </div> <div class="section" id="backward-compatibility"> <h2>Backward compatibility<a class="headerlink" href="#backward-compatibility" title="Permalink to this headline">¶</a></h2> <p>The following syntaxes are allowed for compatibility with previous versions of Routes. They may be removed in the future.</p> <div class="section" id="omitting-the-name-arg"> <h3>Omitting the name arg<a class="headerlink" href="#omitting-the-name-arg" title="Permalink to this headline">¶</a></h3> <p>In the tutorial we said that nameless routes can be defined by passing <tt class="xref docutils literal"><span class="pre">None</span></tt> as the first argument. You can also omit the first argument entirely:</p> <div class="highlight-python"><div class="highlight"><pre><span class="nb">map</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="bp">None</span><span class="p">,</span> <span class="s">"/{controller}/{action}"</span><span class="p">)</span> <span class="nb">map</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s">"/{controller}/{action}"</span><span class="p">)</span> </pre></div> </div> <p>The syntax with <tt class="xref docutils literal"><span class="pre">None</span></tt> is preferred to be forward-compatible with future versions of Routes. It avoids the path argument changing position between the first and second arguments, which is unpythonic.</p> </div> <div class="section" id="varname"> <h3>:varname<a class="headerlink" href="#varname" title="Permalink to this headline">¶</a></h3> <p>Path variables were defined in the format <tt class="docutils literal"><span class="pre">:varname</span></tt> and <tt class="docutils literal"><span class="pre">:(varname)</span></tt> prior to Routes 1.9. The form with parentheses was called “grouping”, used to delimit the variable name from a following letter or number. Thus the old syntax “/:controller/:(id)abc” corresponds to the new syntax “/{controller}/{id}abc”.</p> <p>The older wildcard syntax is <tt class="docutils literal"><span class="pre">*varname</span></tt> or <tt class="docutils literal"><span class="pre">*(varname)</span></tt>:</p> <div class="highlight-python"><div class="highlight"><pre><span class="c"># OK because the following component is static.</span> <span class="nb">map</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s">"/static/*filename/download"</span><span class="p">)</span> <span class="c"># Deprecated syntax. WRONG because the wildcard will eat the rest of the</span> <span class="c"># URL, leaving nothing for the following variable, which will cause the</span> <span class="c"># match to fail.</span> <span class="nb">map</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s">"/static/*filename/:action"</span><span class="p">)</span> </pre></div> </div> </div> <div class="section" id="minimization"> <h3>Minimization<a class="headerlink" href="#minimization" title="Permalink to this headline">¶</a></h3> <p>Minimization was a misfeature which was intended to save typing, but which often resulted in the wrong route being chosen. Old applications that still depend on it must now enable it by putting <tt class="docutils literal"><span class="pre">map.minimization</span> <span class="pre">=</span> <span class="pre">True</span></tt> in their route definitions.</p> <p>Without minimization, the URL must contain values for all path variables in the route:</p> <div class="highlight-python"><div class="highlight"><pre><span class="nb">map</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s">"basic"</span><span class="p">,</span> <span class="s">"/{controller}/{action}"</span><span class="p">,</span> <span class="n">controller</span><span class="o">=</span><span class="s">"mycontroller"</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">"myaction"</span><span class="p">,</span> <span class="n">weather</span><span class="o">=</span><span class="s">"sunny"</span><span class="p">)</span> </pre></div> </div> <p>This route matches any two-component URL, for instance “/help/about”. The resulting routing variables would be:</p> <div class="highlight-python"><div class="highlight"><pre><span class="p">{</span><span class="s">"controller"</span><span class="p">:</span> <span class="s">"help"</span><span class="p">,</span> <span class="s">"action"</span><span class="p">:</span> <span class="s">"about"</span><span class="p">,</span> <span class="s">"weather"</span><span class="p">:</span> <span class="s">"sunny"</span><span class="p">}</span> </pre></div> </div> <p>The path variables are taken from the URL, and any extra variables are added as constants. The extra variables for “controller” and “action” are <em>never used</em> in matching, but are available as default values for generation:</p> <div class="highlight-python"><pre>url("basic", controller="help") => "/help/about?weather=sunny"</pre> </div> <p>With minimization, the same route path would also match shorter URLs such as “/help”, “/foo”, and “/”. Missing values on the right of the URL would be taken from the extra variables. This was intended to lessen the number of routes you had to write. In practice it led to obscure application bugs because sometimes an unexpected route would be matched. Thus Routes 1.9 introduced non-minimization and recommended “map.minimization = False” for all new applications.</p> <p>A corollary problem was generating the wrong route. Routes 1.9 tightened up the rule for generating named routes. If a route name is specified in <tt class="docutils literal"><span class="pre">url()</span></tt> or <tt class="docutils literal"><span class="pre">url_for()</span></tt>, <em>only</em> that named route will be chosen. In previous versions, it might choose another route based on the keyword args.</p> </div> <div class="section" id="implicit-defaults-and-route-memory"> <h3>Implicit defaults and route memory<a class="headerlink" href="#implicit-defaults-and-route-memory" title="Permalink to this headline">¶</a></h3> <p>Implicit defaults worked with minimization to provide automatic default values for the “action” and “id” variables. If a route was defined as <tt class="docutils literal"><span class="pre">map.connect("/{controller}/{action}/{id}")</span> <span class="pre">and</span> <span class="pre">the</span> <span class="pre">URL</span> <span class="pre">"/archives"</span></tt> was requested, Routes would implicitly add <tt class="docutils literal"><span class="pre">action="index",</span> <span class="pre">id=None</span></tt> to the routing variables.</p> <p>To enable implicit defaults, set <tt class="docutils literal"><span class="pre">map.minimization</span> <span class="pre">=</span> <span class="pre">True;</span> <span class="pre">map.explicit</span> <span class="pre">=</span> <span class="pre">False</span></tt>. You can also enable implicit defaults on a per-route basis by setting <tt class="docutils literal"><span class="pre">map.explicit</span> <span class="pre">=</span> <span class="pre">True</span></tt> and defining each route with a keyword argument <tt class="docutils literal"><span class="pre">explicit=False</span></tt>.</p> <p>Previous versions also had implicit default values for “controller”, “action”, and “id”. These are now disabled by default, but can be enabled via <tt class="docutils literal"><span class="pre">map.explicit</span> <span class="pre">=</span> <span class="pre">True</span></tt>. This also enables route memory</p> </div> <div class="section" id="url-for"> <h3>url_for()<a class="headerlink" href="#url-for" title="Permalink to this headline">¶</a></h3> <p><tt class="docutils literal"><span class="pre">url_for</span></tt> was a route generation function which was replaced by the <tt class="docutils literal"><span class="pre">url</span></tt> object. Usage is the same except that <tt class="docutils literal"><span class="pre">url_for</span></tt> uses route memory in some cases and <tt class="docutils literal"><span class="pre">url</span></tt> never does. Route memory is where variables from the current URL (the current request) are injected into the generated URL. To use route memory with <tt class="docutils literal"><span class="pre">url</span></tt>, call <tt class="docutils literal"><span class="pre">url.current()</span></tt> passing the variables you want to override. Any other variables needed by the route will be taken from the current routing variables.</p> <p>In other words, <tt class="docutils literal"><span class="pre">url_for</span></tt> combines <tt class="docutils literal"><span class="pre">url</span></tt> and <tt class="docutils literal"><span class="pre">url.current()</span></tt> into one function. The location of <tt class="docutils literal"><span class="pre">url_for</span></tt> is also different. <tt class="docutils literal"><span class="pre">url_for</span></tt> is properly imported from <tt class="docutils literal"><span class="pre">routes</span></tt>:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">routes</span> <span class="kn">import</span> <span class="n">url_for</span> </pre></div> </div> <p><tt class="docutils literal"><span class="pre">url_for</span></tt> was traditionally imported into WebHelpers, and it’s still used in some tests and in <tt class="docutils literal"><span class="pre">webhelpers.paginate</span></tt>. Many old Pylons applications contain <tt class="docutils literal"><span class="pre">h.url_for()</span></tt> based on its traditional importation to helpers.py. However, its use in new applications is discouraged both because of its ambiguous syntax and because its implementation depends on an ugly singleton.</p> <p>The <tt class="docutils literal"><span class="pre">url</span></tt> object is created by the RoutesMiddleware and inserted into the WSGI environment. Pylons makes it available as <tt class="docutils literal"><span class="pre">pylons.url</span></tt>, and in templates as <tt class="docutils literal"><span class="pre">url</span></tt>.</p> </div> <div class="section" id="redirect-to"> <h3>redirect_to()<a class="headerlink" href="#redirect-to" title="Permalink to this headline">¶</a></h3> <p>This combined <tt class="docutils literal"><span class="pre">url_for</span></tt> with a redirect. Instead, please use your framework’s redirect mechanism with a <tt class="docutils literal"><span class="pre">url</span></tt> call. For instance in Pylons:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">pylons.controllers.util</span> <span class="kn">import</span> <span class="n">redirect</span> <span class="n">redirect</span><span class="p">(</span><span class="n">url</span><span class="p">(</span><span class="s">"login"</span><span class="p">))</span> </pre></div> </div> </div> </div> </div> </div> </div> </div> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h3><a href="contents.html">Table Of Contents</a></h3> <ul> <li><a class="reference external" href="#">Unicode, Redirects, and More</a><ul> <li><a class="reference external" href="#unicode">Unicode</a></li> <li><a class="reference external" href="#redirect-routes">Redirect Routes</a></li> <li><a class="reference external" href="#printing">Printing</a></li> <li><a class="reference external" href="#introspection">Introspection</a></li> <li><a class="reference external" href="#other">Other</a></li> <li><a class="reference external" href="#backward-compatibility">Backward compatibility</a><ul> <li><a class="reference external" href="#omitting-the-name-arg">Omitting the name arg</a></li> <li><a class="reference external" href="#varname">:varname</a></li> <li><a class="reference external" href="#minimization">Minimization</a></li> <li><a class="reference external" href="#implicit-defaults-and-route-memory">Implicit defaults and route memory</a></li> <li><a class="reference external" href="#url-for">url_for()</a></li> <li><a class="reference external" href="#redirect-to">redirect_to()</a></li> </ul> </li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="restful.html" title="previous chapter">RESTful services</a></p> <h4>Next topic</h4> <p class="topless"><a href="glossary.html" title="next chapter">Glossary</a></p> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="_sources/uni_redirect_rest.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" size="18" /> <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="related"> <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="modindex.html" title="Global Module Index" >modules</a> |</li> <li class="right" > <a href="glossary.html" title="Glossary" >next</a> |</li> <li class="right" > <a href="restful.html" title="RESTful services" >previous</a> |</li> <li><a href="index.html">Routes home</a> | </li> <li><a href="contents.html">Documentation</a>»</li> </ul> </div> <div class="footer"> © Copyright 2010, Ben Bangert, Mike Orr. Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.4. </div> </body> </html>