close Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Changes between Version 6 and Version 7 of UrlRouting


Ignore:
Timestamp:
Jul 9, 2008, 11:55:23 AM (13 years ago)
Author:
cmlenz
Comment:

URL generation

Legend:

Unmodified
Added
Removed
Modified
  • UrlRouting

    v6 v7  
    2626 * the '''target''' function that should handle requests matching that pattern (optional).
    2727
    28 Additional indented lines can be provided to set default values for parameters that get passed on to the request handler. The value of these parameters can be any Python expression.
     28Additional indented lines can be provided below any route to set default values for parameters that get passed on to the request handler. The value of these parameters can be any Python expression.
    2929
    3030If no name is specified for a route, URLs for that route can not be generated by name, but only based on the function object. If no target is specified, the route is only used for generating links, not for dispatching requests.
     31
     32== Generating URLs ==
     33
     34The `diva.routing` modules provides a number of ways to generate URLs based on the routing.
     35
     36The central function is `path_to(target, *args, **kwargs)`. Here, `target` is either the name of the route, or the function handling the route. Additional arguments are substituted for variables in the path of the route. Any remaining keyword arguments are added as query string parameters:
     37
     38{{{
     39#!pycon
     40>>> from diva.routing import path_to
     41>>> path_to('info', id=42)
     42/info/42
     43>>> path_to('info', id=42, page=2)
     44/info/42?page=2
     45}}}
     46
     47Use the `abs_url` function to turn such relative URLs into absolute ones including scheme and host:
     48
     49{{{
     50#!pycon
     51>>> from diva.routing import abs_url, path_to
     52>>> abs_url(path_to('info', id=42))
     53http://localhost:8080/info/42
     54}}}
     55
     56Finally, the `redirect(target, *args, **kwargs)` function is a shortcut for redirecting to the absolutized result of `path_to(). It will raise an HTTP exception, and thus immediately break out of the current code.
    3157
    3258== API Documentation ==
    3359
    3460[[PythonDoc(trunk, diva.routing)]]
     61