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.
- Timestamp:
-
Jul 9, 2008, 11:55:23 AM (16 years ago)
- Author:
-
cmlenz
- Comment:
-
URL generation
Legend:
- Unmodified
- Added
- Removed
- Modified
-
v6
|
v7
|
|
26 | 26 | * the '''target''' function that should handle requests matching that pattern (optional). |
27 | 27 | |
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. |
| 28 | Additional 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. |
29 | 29 | |
30 | 30 | If 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 | |
| 34 | The `diva.routing` modules provides a number of ways to generate URLs based on the routing. |
| 35 | |
| 36 | The 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 | |
| 47 | Use 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)) |
| 53 | http://localhost:8080/info/42 |
| 54 | }}} |
| 55 | |
| 56 | Finally, 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. |
31 | 57 | |
32 | 58 | == API Documentation == |
33 | 59 | |
34 | 60 | [[PythonDoc(trunk, diva.routing)]] |
| 61 | |