Table of contents
1.
Introduction 📝
2.
URLs in web2py 📖
2.1.
🔖Important points regarding the URLs in web2py
2.2.
🔖Parameters to the URL function
2.3.
🔖Important points regarding the Parameters to the URL function
2.4.
🔖Static file URL mapping in web2py
3.
Absolute URLs in web2py 🎯
3.1.
Digitally signed URLs in web2py 🎯
3.1.1.
Method 1
3.1.2.
Note that the URL.verify function is used to validate the digital signature. The URL is verified using the hmac key, salt, and hash vars parameters supplied to the URL function when the digital signature was formed. The values of these arguments must match the values that were passed to the URL function when the digital signature was created.Method 2
4.
Frequently Asked Question
4.1.
What do you mean by URL?
4.2.
How do static files are mapped to URLs in web2py?
4.3.
What is the basic structure of URLs?
4.4.
What is web2py used for?
4.5.
Which companies are using the web2py framework?
5.
Conclusion
Last Updated: Mar 27, 2024

URLs in web2py

Author Alok Pandey
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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

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')

Absolute URLs in web2py 🎯

In web2py, by default, URL generates relative URLs. However, we can also generate absolute URLs by specifying the scheme and host arguments. This method is useful, for example, when we are inserting URLs in email messages):

URL(..., scheme='http', host='www.codingninjas.com')

By setting the arguments to True, you may automatically include the current request's scheme and host.

URL(..., scheme=True, host=True)

If necessary, the URL function additionally accepts a port parameter that specifies the server port.

Digitally signed URLs in web2py 🎯

We have the ability to sign a URL when it is generated digitally. A _signature GET variable that the server may validate will be added as a result. There are two methods to go about this.

Method 1

we can pass the following arguments  to the URL function:

hmac_key: the key for signing the URL (It is a string )

salt: an optional string to salt the data before signing

hash_vars: An optional list of GET variables, or names of variables from the URL query string, to be included in the signature. Additionally, it may be adjusted to either True (the default), which includes all variables, or False, which excludes all variables.

Example

KEY = 'anykey'

def one():
    return dict(link=URL('two', vars=dict(d=145), hmac_key=KEY))

def two():
    if not URL.verify(request, hmac_key=KEY): raise HTTP(403)
    # do something
    return locals()
You can also try this code with Online Python Compiler
Run Code

A digitally signed URL looks like this:

/application/default/two?d=145&_signature=4981bc70e13866bb60e52a09073560ae822224e9

Note that the URL.verify function is used to validate the digital signature. The URL is verified using the hmac key, salt, and hash vars parameters supplied to the URL function when the digital signature was formed. The values of these arguments must match the values that were passed to the URL function when the digital signature was created.

Method 2

The second method is more sophisticated. This method mainly uses the digitally signed URLs in conjunction with Auth.

Example:

@auth.requires_login()
def one():
    return dict(link=URL('two', vars=dict(a=145), user_signature=True)

@auth.requires_signature()
def two():
    # do something
    return locals()
You can also try this code with Online Python Compiler
Run Code

In the above example, the session shares an automatically produced hmac key. As a result, action two can provide action one access control authority. The link is genuine if it has been generated and signed; else, it is invalid. The link will be invalid if it is taken by another user.

Always digitally signing Ajax callbacks is a good idea. You may utilize the user signature parameter from the web2py LOAD function for this if you do so:

{{=LOAD('default', 'two', vars=dict(a=145), ajax=True, user_signature=True)}}

Frequently Asked Question

What do you mean by URL?

A Uniform resource locator 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.

How do static files are mapped to URLs 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.

What is the basic structure of URLs?

The Uniform resource locator follows a general structure i.e. protocol://domain/path.

What is web2py used for?

Web2py allows web developers to use Python to create dynamic online content. Web2py is intended to assist decrease time-consuming web development activities such as creating web forms from scratch, while a web developer may create a form from scratch if necessary.

Which companies are using the web2py framework?

Vidjil, Sarapis, StopStalk, Groupthink, Rune Interactive, Oceangrafix.com, and Food2Fork are a few famous companies using web2py.

Conclusion

In this article, we have extensively discussed URLs in web2py. We discussed building a URL function and what arguments go into that function. We also saw Absolute and Digitally signed URLs in web2py. If you would like to learn more, check out our articles on 

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. 

Enroll in our courses and refer to the mock test and problems available.

Take a look at the interview experiences and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow. 

Happy Coding!

Live masterclass