Introduction 📝
A Uniform resource locator(URL) is the address of a given unique resource on the Web. Every valid URL points to a specific resource. These resources can be multimedia files or text or pdf files. In this blog, we will discuss URLs in web2py. We also cover the topic of how to define a URL function and what are the parameters given to that URL function. We also see Absolute and Digitally signed URLs in web2py.

URLs in web2py 📖
A Uniform resource locator (URL) is a function that plays a vital role in the web2py project. It decides the path for action and static files. Let's understand this with an example:
URL('k')
Is mapped into
/[application]/[controller]/f
In the above example, it is clear how a URL is mapped in web2py. First, it goes through the application then the controller, and then the path provided by the user.
🔖Important points regarding the URLs in web2py
URLs in web2py decide the path for action and static files. There are many more things that we should keep in mind while dealing with URLs in web2py, few of them are listed below:
-
The URL function's output is determined by the current application's name, the caller controller, and other arguments.
-
URL mapping permits you to redefine the format of external URLs.
-
web2py supports both URL mapping and reverse URL mapping.
- Links inside the web2py application won't break if you construct all of the internal URLs using the URL function rather than manually editing URL mappings.
🔖Parameters to the URL function
We can pass additional parameters to the URL function in web2py, i.e., additional terms in the URL path (args) and URL query variables (vars):
URL('k', args=['a', 'b'], vars=dict(z='p'))
is mapped into
/[application]/[controller]/k/a/b?z=p
🔖Important points regarding the Parameters to the URL function
We can pass additional parameters to the URL function in web2py but we should take care of the following points:
-
Web2py automatically decodes, parses, and stores the args attributes as request.args.
-
The main method through which web2py communicates data with the client's browser is via args and vars, which are processed, encoded, and then saved in request.vars.There is no need to send args as a list if there is just one element present.
-
we can also use the URL function to generate URLs to actions in other controllers and other applications:
URL('p', 'q', 'k', args=['x', 'y'], vars=dict(z='m'))
is mapped into
/p/q/k/x/y?z=m
-
We can also specify the application, controller, and function using named arguments:
URL(p='p', q='q', k='k')
If the application name p is missing the current app is assumed.
URL('q', 'k')
-
If the controller name q is missing, the current one is assumed.
URL('k')
-
We can pass the function itself instead of the name of a controller function.
URL(k)
🔖Static file URL mapping in web2py
When creating URLs for static files for our apps, we should always utilize the URL function. When uploaded using the administrative interface, static files go into the application's static subdirectory, where they are kept. A virtual "static" controller is provided by web2py whose function is to get files from the static subdirectory, ascertain their content type, and stream the file to the client. The URL for the static file "sampleimage.png" is generated using the example below:
URL('static', 'sampleimage.png')
is mapped into
/[application]/static/sampleimage.png
If the static file is in a subdirectory within the static folder, the subfolder(s) might be included in the filename. For instance, to generate:
/[application]/static/images/icons/cat.png
one should use:
URL('static', 'images/icons/cat.png')






