Jarrod
Jarrod

Reputation: 1705

web2py Ajax search

I'm trying to use an ajax search slice for my website that I found here: http://www.web2pyslices.com/slices/take_slice/51

But for some reason I keep getting the error:

IndexError: list index out of range

Here is my version of the code:

default.py (controller)

def index():
listings = db().select(db.listing.ALL, orderby=db.listing.first_name)

return dict(listings=listings, livesearch=livesearch())

def livesearch():
    partialstr = request.vars.values()[0]
    query = db.listing.title.like('%'+partialstr+'%')
    listings = db(query).select(db.listing.title)
    items = []

    for (i,listing) in enumerate(listings):
        items.append(DIV(A(listing.title, _id="res%s"%i, _href="#", _onclick="copyToBox($('#res%s').html())"%i), _id="resultLiveSearch"))

    return TAG[''](*items)

livesearch.html (view, which I'm {{including}} in the layout.html

<input type="text" id="search" name="search" autocomplete="off" onkeyup="getData(this.value);" /><br />
<div id="ajaxresults"></div>

db.py (model)

db.define_table(auth.settings.table_user_name,
            Field('first_name'),
            Field('last_name'),
            Field('email'),
            Field('password','password', length=512, readable=False, label='Password'),
            Field('title'),
            Field('photo','upload'),
            Field('bio','text'),
            Field('phone'), # Contact details
            Field('website'),
            Field('address'),
            Field('registration_key', length=512,
                writable=False, readable=False, default=''),
            Field('reset_password_key', length=512,
                writable=False, readable=False, default=''),
            Field('registration_id', length=512,
                writable=False, readable=False, default=''),
            )

listing = db[auth.settings.table_user_name]

Any help would be very very greatly appreciated, cause I've been wracking my brains on it for days now (because I'm extremely new to programming)

Thanks!

Upvotes: 1

Views: 2559

Answers (2)

Anthony
Anthony

Reputation: 25536

def index():
    listings = db().select(db.listing.ALL, orderby=db.listing.first_name)
    return dict(listings=listings, livesearch=livesearch())

You don't want to return livesearch from the index function. According to the slice you referenced, the livesearch function should be called via Ajax from your index page.

def livesearch():
    partialstr = request.vars.values()[0]

I know the above line is taken directly from the slice, but a better (and more typical way) to access the value of the posted variable is:

partialstr = request.vars.partialstr if request.vars else None

Note, the above syntax will return None if there are no request.vars or if request.vars.partialstr doesn't exist, so it won't generate an error.

Also, request.vars will be None whenever there are no request variables, so you can always test for request variables with:

if request.vars:

Finally, you may be interested in web2py's built-in auto-complete widget (though I think there may be some problems with it in IE, for which a fix is in the works).

Upvotes: 1

HYRY
HYRY

Reputation: 97331

If the following is your index() code:

def index():
    listings = db().select(db.listing.ALL, orderby=db.listing.first_name)
    return dict(listings=listings, livesearch=livesearch())

then, if you visit index.html page and livesearch() will be called, but at this time, request.vars.values() is empty, so IndexError raised.

Don't call livesearch() in index(), and use ajax to post search word to livesearch.html, and web2py will call livesearch(), and request.vars.values()[0] is the search word.

Upvotes: 1

Related Questions