Search code examples
web2py

How to use request.vars in web2py?


I am trying to understand how request.vars works in web2py. I 'd appreciate some help.

def one():
    x = 5
    return dict(x=x)

If i want to get the value of variable x in an other function do I use request.vars? I tried this way but of course, it didn't work:

def two():
    y = request.vars.x
    return locals() 

Solution

  • request.vars contains parameters passed in the body of an HTTP request (usually a GET or POST request derived from submitting a form).

    In your example, once you've defined x you could redirect from one to two while passing x in a variable called value:

    redirect(URL('two', vars=dict(value=x)))
    

    Then in two you could access the value of x via request.vars['value'].

    There is some additional detail in the documentation here: http://web2py.com/books/default/chapter/29/04/the-core#request