Python web2pyajax搜索
我正在尝试为我的网站使用ajax搜索片段,我在这里找到: 但出于某种原因,我不断地得到错误:Python web2pyajax搜索,python,ajax,search,web2py,Python,Ajax,Search,Web2py,我正在尝试为我的网站使用ajax搜索片段,我在这里找到: 但出于某种原因,我不断地得到错误: IndexError: list index out of range 以下是我的代码版本: default.py(控制器) livesearch.html(视图,我在layout.html中{{{include}}} <input type="text" id="search" name="search" autocomplete="off" onkeyup="getData(this.val
IndexError: list index out of range
以下是我的代码版本:
default.py(控制器)
livesearch.html(视图,我在layout.html中{{{include}}}
<input type="text" id="search" name="search" autocomplete="off" onkeyup="getData(this.value);" /><br />
<div id="ajaxresults"></div>
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]
任何帮助都将不胜感激,因为我已经为此绞尽脑汁好几天了(因为我对编程非常陌生)
谢谢!如果以下是您的index()代码: 然后,如果访问index.html页面,将调用livesearch(),但此时request.vars.values()为空,因此引发Indexer错误 不要在index()中调用livesearch(),使用ajax将搜索词发布到livesearch.html,web2py将调用livesearch(),request.vars.values()[0]是搜索词
def index():
listings = db().select(db.listing.ALL, orderby=db.listing.first_name)
return dict(listings=listings, livesearch=livesearch())
您不想从index
函数返回livesearch
。根据您引用的,应从index
页面通过Ajax调用livesearch
函数
def livesearch():
partialstr = request.vars.values()[0]
我知道上面的行是直接从切片中获取的,但是访问POST变量值的更好(更典型的方法)是:
partialstr = request.vars.partialstr if request.vars else None
注意,如果没有request.vars
或request.vars.partialstr
不存在,则上述语法将返回None
,因此不会生成错误
此外,只要没有请求变量,request.vars
将是None
,因此您可以始终使用以下方法测试请求变量:
if request.vars:
最后,您可能会对web2py的内置功能感兴趣(尽管我认为IE中可能存在一些问题,正在对此进行修复)。非常感谢您!!我是一个真正的初学者,为此我花了好几个小时。此代码修复了我的问题:“partialstr=request.vars.partialstr”
partialstr = request.vars.partialstr if request.vars else None
if request.vars: