【发布时间】:2015-09-01 07:04:50
【问题描述】:
在选择国家名称并按下按钮后,我试图获取国家信息。我正在使用cherrypy、jinja、python和mysql。所以我希望信息显示在浏览器上。
但是当我按下提交按钮时,我得到了这个错误:
countList=self.get_info(countryInfo)[0]
IndexError: list index out of range
这是导致问题的代码的主要部分。
@cherrypy.expose
def get_Country_Info(self,countryInfo=None):
self.get_Names()
countList=self.get_info(countryInfo)[0]
tmpl=env.get_template("submit_test.html")
return tmpl.render(salutation="Welcome to", target="Country List", myList=self.country_list, country=countList )
def get_info(self,countryInfo):
self.con.execute("SELECT Code, Name, Continent, Population, Capital FROM Country WHERE Name LIKE %s", (countryInfo,))
self.countryDB=list(self.con.fetchall())
return self.countryDB
【问题讨论】:
-
self.con.fetchall()返回的结果集是空的吗?如果您将一个空列表投射到list它将成为[]并且索引这将超出范围
标签: python mysql cherrypy jinja2