Web2py controllers with parameters?
Asked Answered
J

3

10

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?

Justis answered 9/6, 2010 at 6:12 Comment(0)
D
12

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.

Derosier answered 9/6, 2010 at 7:19 Comment(3)
thanks... I've been figuring this out through trial & error. would be nice if there was some option to change behavior depending on how you wanted it to workJustis
The above isn't true in the case of @service decorated functions. A controller function with parameters can be valid when wrapped in this decorator. This isn't the exact OP scenario but may provide an alternative solution to his problem; see my answer below.Piderit
yes book is http://127.0.0.1:8000/a/c/f.html/x/y/z?p=1&q=2, but i get the parameters dist(request.vars) ¿ is bad practice ?Wheeled
F
2

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

Fornicate answered 9/6, 2010 at 15:44 Comment(1)
hey that's cool! I am a python newbie... so didn't realize that kinda initialization was possible. Thanks!Justis
P
1

There is a circumstance in which web2py controllers can use parameters. When a controller function has the @service decorator, parameters can be used, depending on the service type, for example:

@service.jsonrpc
def somefunction(a=None, b='default'):
    ## whatever

This approach is for when a controller function is really an API, not a way to generate a web view. There are nice things you can do in terms of defining web-view and API-style functions in parallel, and having the web views call the API functions, to ensure you have a good separation of views & controllers.

Depending on how you choose to divide responsibilities among the web client / javascript, the web2py view, and the web2py controller, it can make sense to have controller functions that are truly APIs (with optional parameters) rather than building the parameter-unpacking logic in a web-view style controller.

Piderit answered 17/6, 2013 at 13:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.