Simple search and select / option

5 views
Skip to first unread message

Ross

unread,
Sep 5, 2008, 5:25:49 PM9/5/08
to web2py Web Framework
I have been trying Web2Py and fine it relatively fast and simple.
Seems like a very good framework. It really stands out.

After a basic search, I have not found very clear and specific
documentation regarding a simple search and select / option.
I was wondering if there was a detailed document or if anyone could
provide details on how to

1) Make a simple search with the database that the user can perform.
Let's say I have a contacts database and I want the user to be able to
search by last name or contact type.

How would I set up the form ( view or controller ?) to access the
database for this variable ? Which files and what code would I need to
make.

2) I have seen some documentation on the select / option. What would
be the sequence and the files where I would set up the select option ?
Lets say I want to have a select option for contact type e.g.
personal , client, vendor, etc. ?

Thanks
Ross

Massimo Di Pierro

unread,
Sep 5, 2008, 6:45:00 PM9/5/08
to web...@googlegroups.com
Hi Ross,

say you have

db=SQLDB('sqlite://storage.db')
db.define_table('contact',SQLField('name'),SQLField('category'))
db.contact.name.requires=IS_NOT_EMPTY()
db.contact.category.requires=IS_IN_SET(['friend','enemy'])

here are three possible searches controllers:

def search_by_name():
form=SQLFORM(db.contact,fields=['name'])
if request.vars.name: rows=db(db.contact.name.like('%%%s%%' %
request.vars.name)).select(orderby=db.contact.name)
else: rows=[]
return dict(form=form,rows=rows)

def search_by_category():
form=SQLFORM(db.contact,fields=['category'])
if request.vars.category: rows=db
(db.contact.category==request.vars.category).select
(orderby=db.contact.name)
else: rows=[]
return dict(form=form,rows=rows)

def search_by_name_and_category():
form=SQLFORM(db.contact,fields=['name','category'])
s=db
if request.vars.name: s=s(db.contact.name.like('%%%s%%' %
request.vars.name))
if request.vars.category: s=s
(db.contact.category==request.vars.category)
if request.vars: rows=s.select(orderby=db.contact.name)
else: rows=[]
return dict(form=form,rows=rows)

hope this help. Hope no typoe.

Massimo

NoviceSortOf

unread,
Nov 2, 2008, 5:56:21 AM11/2/08
to web2py Web Framework

After reviewing your examples above, I've tried the following,
but it returns "Invalid request invalid function".

Unfortunately I'm unable to tell which line or perhaps
if the syntax is incorrect.

* Is it possible to test functions like SQLFORM
on a command line, or in IDLE or other debug
editor suite?

* Is there some error or wrong assumption in my
code or syntax?


def search_by_author():
form=SQLFORM(db.titles,fields=['author'])
if request.vars.author: rows=db(db.titles.author.like('%%%s%%' %
request.vars.author)).select(orderby=db.titles.author)
else: rows=[]
return dict(form=form,rows=rows)

mdipierro

unread,
Nov 2, 2008, 10:11:10 AM11/2/08
to web2py Web Framework
On Nov 2, 4:56 am, NoviceSortOf <jons...@well.com> wrote:
> After reviewing your examples above, I've tried the following,
> but it returns "Invalid request invalid function".

That mean you are calling an action that does not exist. There is a
typo in the URL.

> * Is it possible to test functions like SQLFORM
> on a command line, or in IDLE or other debug
> editor suite?

yes,

python web2py.py -S yourappname -M
>>> print db.tables
>>> form=SQLFORM(db[db.tables[0]])
etc.

> * Is there some error or wrong assumption in my
> code or syntax?
>
> def search_by_author():
> form=SQLFORM(db.titles,fields=['author'])
> if request.vars.author: rows=db(db.titles.author.like('%%%s%%' %
> request.vars.author)).select(orderby=db.titles.author)
> else: rows=[]
> return dict(form=form,rows=rows)

I do not think there is an error in this code. I think you have a typo
in the code that calls it.

Massimo
Reply all
Reply to author
Forward
0 new messages