web2py Ajax search

大憨熊 提交于 2019-12-08 07:24:30
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).

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!