Newbie Web2Py Questions

42 views
Skip to first unread message

Christopher Power

unread,
May 17, 2014, 8:23:33 AM5/17/14
to web...@googlegroups.com
Hi all,

I'm a relatively new user of Web2Py.  I looked at it about a year ago and loved a lot of its features, but ended up having to teach PHP to my undergrads for a variety of reasons that are too boring to go into.  Blech.

Anyways ... this year I'm insisting on using Web2Py for my course. I'm also going to be using it on a few of my own projects to get up to speed on the idiosyncrasies on the framework.  I've been successful doing some relatively complex queries and pulling data from some ugly legacy database, so that has all gone pretty well.

With all of that boring information out of the way - I'm looking for a bit of advice on a few things (probably the first of many).  Hopefully these questions aren't too basic - I did a search and didn't find anything.

1) Is there a way that I can return multiple dict's from a Controller to a View?  I have a situation where I need to return the results of two different queries back to the
view for display.  I certainly can do that by manipulating the dict's and slamming them together - but is there a way to return multiple dicts as in regular python?  When I tried the python way or returning multiple dicts the server just couldn't return anything (even an error) - I just got a 303.

2) I currently have information pulling from a collection of documents called a cartulary (long story on that project).  So when I select from default/index the name
of a collection I go from:
to

As expected, where 0001 is the identifier for the cartulary.

Now, on that page is a list of documents.  I would like to have the situation where if the user selects a document id, the system displays a page of the document at
the URL:

Where 1 is the id of the document in the collection.  How is the best way to do this?  Right now I have it directing to /default/documents/1 through the default controller and that is okay - but not quite what I want.

3) I teach my students test driven development.  Before I start diving into it myself - is there anything built into Web2Py that I've missed for doing test driven development or the related behaviour driven development?  

Cheers
Chris

Massimo Di Pierro

unread,
May 18, 2014, 10:26:40 AM5/18/14
to web...@googlegroups.com
1) You can return anything your want:

def index():
    a = {}
    b = {}
    return {'a':a, 'b':b}

2) Not really but you should look into gluon/contrib/webclient.py and doctests. Web2py runs doctests in controllers automatically.

Anthony

unread,
May 18, 2014, 11:31:08 AM5/18/14
to web...@googlegroups.com
2) I currently have information pulling from a collection of documents called a cartulary (long story on that project).  So when I select from default/index the name
of a collection I go from:
to

As expected, where 0001 is the identifier for the cartulary.

Now, on that page is a list of documents.  I would like to have the situation where if the user selects a document id, the system displays a page of the document at
the URL:

Where 1 is the id of the document in the collection.  How is the best way to do this?  Right now I have it directing to /default/documents/1 through the default controller and that is okay - but not quite what I want.

In the cartulary action, just include some logic to check request.args, and return different content depending on the args:

def cartulary():
   
if request.args(1) == 'document':
        doc_id
= request.args(2)
       
[return document doc_id from cartulary request.args(0)]
   
else:
       
[return list of documents for cartulary request.args(0)]

3) I teach my students test driven development.  Before I start diving into it myself - is there anything built into Web2Py that I've missed for doing test driven development or the related behaviour driven development?

In addition to what Massimo pointed out, have a look at https://github.com/viniciusban/web2py.test and https://github.com/niphlod/welcome_augmented.

Anthony
Reply all
Reply to author
Forward
0 new messages