URL function generates internal URL paths for the actions and the static files.
Here is an example:
URL('f')
is mapped into
/[application]/[controller]/f
Notice that the output of the URL function depends on the name of the current application, the calling controller, and other parameters. web2py supports URL mapping and reverse URL mapping. URL mapping allows you to redefine the format of external URLs. If you use the URL function to generate all the internal URLs, then additions or changes to URL mappings will prevent broken links within the web2py application.
You can also use the URL function to generate URLs to actions in other controllers and other applications:
It is also possible to specify application, controller and function using named arguments:
URL(a='a', c='c', f='f')
If the application name a is missing the current app is assumed.
URL('c', 'f')
If the controller name c is missing, the current one is assumed.
URL('f')
Instead of passing the name of a controller function it is also possible to pass the function itself
URL(f)
For the reasons mentioned above, you should always use the URL function to generate URLs of static files for your applications. Static files are stored in the application's static subfolder (that's where they go when uploaded using the administrative interface). web2py provides a virtual 'static' controller whose job is to retrieve files from the static subfolder, determine their content-type, and stream the file to the client. The following example generates the URL for the static file "image.png":
URL('static', 'image.png')
is mapped into
/[application]/static/image.png
If the static file is in a subfolder within the static folder, you can include the subfolder(s) as part of the filename. For example, to generate:
/[application]/static/images/icons/arrow.png
one should use:
URL('static', 'images/icons/arrow.png')
You do not need to encode/escape the args and vars arguments; this is done automatically for you.
The vars are parsed, decoded, and then stored in request.vars. args and vars provide the basic mechanism by which web2py exchanges information with the client's browser.
By default, the extension corresponding to the current request (which can be found in request.extension) is appended to the function, unless request.extension is html, the default.
This can be overridden by explicitly including an extension as part of the function name URL(f='name.ext') or with the extension argument:
The current extension can be explicitly suppressed:
By default, URL generates relative URLs. However, you can also generate absolute URLs by specifying the scheme and host arguments (this is useful, for example, when inserting URLs in email messages):
You can automatically include the scheme and host of the current request by simply setting the arguments to True.
By default, URL generates relative URLs. However, you can also generate absolute URLs by specifying the scheme and host arguments (this is useful, for example, when inserting URLs in email messages):
You can automatically include the scheme and host of the current request by simply setting the arguments to True.
An optional list of names of variables from the URL query string (i.e., GET variables) to be included in the signature. It can also be set to True (the default) to include all variables, or False to include none of the variables.