Generate URLs with the url() function in views
**********************************************
:Author: Peter Bengtsson, Jan-Jaap Driessen
The 'url()' function makes it really easy to generate a URL for an object or by name or a combination of an object and a name. We'll also show how to append a query string to the URL by passing a dictionary to the 'url()' function.
Purpose
=======
You want to easily construct URLs in your view classes and view templates.
Prerequisities
==============
A running Grok site with one or more views.
Step by step
============
The url() function is really easy to use and you should be able to throw it an object, a name or neither and it will construct the URL for you. If you omit the object parameter it will default to "self" where "self" is either the object you're doing this in or the view you're calling it form. As an added bonus you can use url() to construct a URL with a CGI query string that is URL encoded.
Suppose you have a view class called "FooView" and an accompanying template file called fooview.pt. If you want to add a URL to the template itself you can do that with this TAL code.
The following combinations of object, name and data are allowed:
.. code-block:: html
Link to this page
This is the same as using the python: syntax:
.. code-block:: html
Link to this page
When not specifying the object, url() defaults to the `view` itself:
.. code-block:: html
Link to this page
To make a url to the container of the view, use `context`:
.. code-block:: html
Link to the container
To make a url to a different view on the current container, use `context` and name:
.. code-block:: html
Link to adding to the container
Using the `data` argument, a query string is appended to the generated URL:
.. code-block:: html
Link to adding to the container, with a=1&b=2
You can supply unicode and lists in the data argument. These are properly escaped.
.. code-block:: html
?name=Pj%C3%B6tr
.. code-block:: html
?id=1&id=2&id=3
.. code-block:: html
?name=Pj%C3%B6tr&name=Sonja
Further information
===================
You can still use Zope's automatic type casting of parameters:
.. code-block:: html
?amount%3Aint=25