我试图使用AJAX搜索切片为我的网站,我发现在这里: HTTP: //www.web2pyslices/slices/take_slice/51
但由于某些原因,我不断收到错误:
IndexError:列表索引超出范围下面是我的版本的code:
default.py(控制器)
高清指数(): 房源= DB()。选择(db.listing.ALL,排序依据= db.listing.first_name) 返回字典(目录=目录,livesearch = livesearch()) 高清livesearch(): partialstr = request.vars.values()[0] 查询= db.listing.title.like('%'+ partialstr +'%') 房源= DB(查询)。选择(db.listing.title) 项= [] 对(I,上市)的枚举(列表): items.append(DIV(A(listing.title,_id =RES%的%我,_href =#,_onclick =copyToBox($('#RES%s'的)。HTML())%I ),_id =resultLiveSearch)) 返回TAG [''](*项)
livesearch.html(视图,我在{{包括}}中的layout.html
<输入类型=文本ID =搜索NAME =搜索自动完成=关闭的onkeyup =的getData(THIS.VALUE); />< BR /> < DIV ID =ajaxresults>< / DIV>
db.py(模型)
db.define_table(auth.settings.table_user_name, 现场(FIRST_NAME), 现场('姓氏'), 现场(电子邮件), 字段(密码,密码,长度= 512,可读=假,标签='密码'), 现场(标题), 现场(照片,上传), 现场('生物','文字'), 现场(手机),#联系方式 现场(网站), 现场(地址), 现场('registration_key',长度= 512, 写=假,可读=假,默认=''), 现场('reset_password_key',长度= 512, 写=假,可读=假,默认=''), 现场('registration_id',长度= 512, 写=假,可读=假,默认=''), ) 上市= DB [auth.settings.table_user_name]
任何帮助将是非常非常大幅度AP preciated,因为我一直令人头大我的脑子就可以了好几天了(因为我非常新的节目)
谢谢!
解决方案 高清指数(): 房源= DB()。选择(db.listing.ALL,排序依据= db.listing.first_name) 返回字典(目录=目录,livesearch = livesearch())您不想回 livesearch 从首页功能。据你引用的片,在 livesearch 功能应通过Ajax从指数名为页。
高清livesearch(): partialstr = request.vars.values()[0]
我知道上述线从片直接服用,但更好的(和更典型的方式)来访问张贴变量的值是:
partialstr = request.vars.partialstr如果request.vars其他无请注意,上面的语法将返回无如果没有 request.vars 或者 request.vars.partialstr 不存在,所以不会产生错误。
此外, request.vars 将无每当有没有请求变量,让你随时可以测试与请求变量:
如果request.vars:
最后,你可能会感兴趣的web2py内置的自动完成小部件(虽然我觉得可能有一些问题,它在IE中,对于这一个解决办法是在作品中)。
I'm trying to use an ajax search slice for my website that I found here: www.web2pyslices/slices/take_slice/51
But for some reason I keep getting the error:
IndexError: list index out of rangeHere 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!
解决方案 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 NoneNote, 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).
更多推荐
web2py的AJAX搜索
发布评论