On Wednesday, September 17, 2014 9:12:11 AM UTC-4, Maurice Waka wrote:
>
> Hi
> 1. I note that the code: 
> match_row = db(db.keywords.x.contains('|%s|' % keyword)).select().first(). 
> This picks the first item in he list. How about a random search through the 
> list without using select().first()?
>

db(db.keywords.x.contains('|%s|' % keyword)).select(orderby='<random>').
first()
 

> 2. In my code:
>  def types:
>     db = DAL('sqlite.storage.db')
>     db.define_table('types'
>                     Field('body'))
>     rows = db(db.types.body.id>0)select()
>     for item in row:
>         item = item
>         return item.
> I GET AN ERROR: NoneType item not iterable. I want the user to put in any 
> data e.g. '123' and if boolean(True) it prints out the answer.I am using 
> this code on a ython module imported to web2py and not in the controller. 
> In my view: {{=item}}
>

A few problems:

   - Should be db.types.id >0, not db.types.body.id > 0.
   - If you have "return item" in your for loop, it will simply return 
   during the first iteration of the loop. Instead, you should just return the 
   Rows object and let the view iterate and display all the items.
   - If this code is in a module, the return value of the function will not 
   be available in a view. When a controller function returns a dict, the keys 
   in the dict become global variables in the associated view. So, if you want 
   to pass something from a function in a module to a view, the module 
   function should be called by the controller function (alternatively, the 
   view can directly import and call the module function, but that pattern is 
   discouraged -- try to keep the views to presentation logic).

It might help if you spend a little more time with the web2py documentation 
to better understand how everything works together.

Anthony

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to