tags:

views:

110

answers:

2

I am building an app using Web2py framework... I don't want to have to use the request object to get all of the querystring parameters, instead I'd like to build my controller with named parameters and have the router unpack the querystring (or form data) dictionary into the named parameters and call my controller.

so instead of a controller method of

create_user():

where I would use the global request() object and look through the vars list... I would prefer instead to have

create_user(first_name, last_name, email):

like I see in other MVC platforms.

is this possible in Web2py already? or is there a plugin for it? or do I need to add that myself?

+2  A: 

No. As stated in the book, an URL of the form

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

maps to application (folder) a, controller (file) c.py, function f, and the additional arguments must be unpacked from the request object as

x, y, z = tuple(request.args)
p = request.vars['p'] # p=1
q = request.vars['q'] # q=2 

Furthermore, web2py specifically detects valid controller functions as those functions that have no arguments. AFAICR, this is opposite to Django which detects valid controller functions as those that have at least one argument.

cjrh
Nick Franceschina
+1  A: 

I do

def create_user():
    try:
        first_name, last_name, email = request.args[:3]
    except:
        redirect('some_error_page')

but mind that first_name, last_name and email may contain chars that are not allowed in the path_info (web2py in picky when validating that only [\w\-\.] are allowed).

mdipierro
hey that's cool! I am a python newbie... so didn't realize that kinda initialization was possible. Thanks!
Nick Franceschina