Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web2py controllers with parameters?

Tags:

python

web2py

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?

like image 384
Nick Franceschina Avatar asked Jun 09 '10 06:06

Nick Franceschina


People also ask

How do I use web2py?

for the different applications that you create. To start your Web2py web server, run the web2py.exe from your extracted files and it will then ask you to set up an administrative password to access your applications at a later point in time. Clicking on start server will open your web2py applications in the browser.

Is web2py secure?

web2py is built for security. This means that it automatically addresses many of the issues that can lead to security vulnerabilities, by following well established practices.

Is web2py an MVC framework?

The design of web2py was inspired by the Ruby on Rails and Django frameworks. Like these frameworks, web2py focuses on rapid development, favors convention over configuration approach and follows a model–view–controller (MVC) architectural pattern.


2 Answers

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.

like image 117
Caleb Hattingh Avatar answered Nov 09 '22 01:11

Caleb Hattingh


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

like image 32
mdipierro Avatar answered Nov 09 '22 02:11

mdipierro