• 18482 views
  • 0 comments
  • loading...

the most important functions in web2py

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:

URL('a', 'c', 'f', args=['x', 'y'], vars=dict(z='t'))
is mapped into
/a/c/f/x/y?z=t
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.


URL()

Attributes / Arguments

You can pass additional parameters to the URL function, i.e., extra terms in the URL path (args) and URL query variables (vars).

The args attributes are automatically parsed, decoded, and finally stored in request.args by web2py. 

If args contains only one element, there is no need to pass it in a list.

URL('f', args=['x', 'y'], vars=dict(z='t'))
# is mapped into
>> /[application]/[controller]/f/x/y?z=t

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.

URL('f', args=['x', 'y'], vars=dict(z='t'))

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:

URL(..., extension='css')
URL(..., extension=False)

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.


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

URL(..., scheme=True, host=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.

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

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

The key for signing the URL (a string).

KEY = 'mykey'

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

def two():
    if not URL.verify(request, hmac_key=KEY): raise HTTP(403)
    # do something
    return locals()

An optional string to salt the data before signing

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.

A second and more sophisticated but more common use of digitally signed URLs is in conjunction with Auth.

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

@auth.requires_signature()
def two():
    # do something
    return locals()


loading...

Sources / Reference

" web2py Book Chapter 4: The core" is licensed under Creative Common License BY-NC-ND 3.0


Comments

loading...

Powered by
Web2py

Hosted on
www.pythonanywhere.com
(affiliated link)