Web2py URL Mapping

URL Mapping from the Book Chapter 4 – Dispatching. These are the basics there’s more detail in the book.

The URL    http://site.com/a/c/f    maps to:

The function f() in controller “c.py” in application “a“.

  • If f is not present, web2py defaults to the index controller function.
  • If c is not present, web2py defaults to the default.py controller.
  • If a is not present, web2py defaults to the init application.
  • If there is no init application, web2py tries to run the welcome application.
  • The extension .html is optional.
  • The extension .html is assumed as default.
  • The extension determines the extension of the view that renders the output of the controller function f().
  • The extension allows the same content to be served in multiple formats (html, xml, json, rss, etc.).

Web2py maps GET/POST requests of the form:

http://site.com/a/c/f.html/x/y/z?p=1&q=2

As before, to function f in controller “c.py” in application a

and it stores the URL parameters in the requestvariable as follows:

    request.args = ['x', 'y', 'z']
    request.vars = {'p':1, 'q':2}
    request.application = 'a'
    request.controller = 'c'
    request.function = 'f'

    request.url = url of request
    request.ajax = False  #by default
    if request.ajax == True and wasInitiatedByAWeb2pyComponent:
        request.cid = componentName
Advertisements

5 thoughts on “Web2py URL Mapping

  1. Joe, I am a complete newbie to python and web2py, and I have been trying to wrap my head around request.args (…) and request.args[…]. You have provided the clearest explanation so far, but could you use a real life url example in the format of [http://site.com/a/c/f.html/x/y/z?p=1&q=2].

    • Dear Rafer,
      Thanks for the comment.

      I am a bit unclear on your question. You said:

      I have been trying to wrap my head around request.args (…) and request.args[…].

      Did you mean?
      request.args = [‘x’, ‘y’, ‘z’]
      and
      request.vars = {‘p’:1, ‘q’:2}
      # Note: request.vars is a Python dict. using braces { } NOT parentheses ( )

      Please clarify and I’ll try to answer your question.
      Also the link you included in your comment comes up broken.

      Thanks again, Rafer. I look forward to your next comment.

      Love and peace,
      Joe

  2. Dear Joe,
    I have to pass arguments to my homepage. Need your help in finding the solution for the same.
    Example : Say i have a website http://www.test.com and i want to pass argument username to this. http://www.test.com/test1 should display some info about user test1. Similarly for test2, test3 etc.. To achieve i need username as argument to http://www.test.com.

    How can i achieve this with web2py controller and view. Please help me..

    • Dear Raghavendra,

      Have a look in the book at URL. It is “one of the most important functions in web2py. It generates internal URL paths for the actions and the static files.”

      Further down the page see:


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

      is mapped into

      /a/c/f/x/y?z=t

      The URL args parameter is a list and should do what you want it to do.

      Love and peace,
      Joe

  3. Pingback: Useful Links for web2py Beginners | pythonmarketer

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s