【发布时间】:2016-03-31 12:05:22
【问题描述】:
我正在为我的python开发学习cherrypy框架。我创建了一个数据库,插入了一些数据,现在我想检索它并在浏览器中显示它。数据是在sqlite数据库中创建的,但是在显示数据时它只是显示for 循环而不是在浏览器中打印数据。
import sqlite3
import cherrypy
class simple_db(object):
@cherrypy.expose
def index(self):
db = sqlite3.connect('picnic.db')
c = db.cursor()
c.execute("SELECT item,quant FROM picnic")
data = c.fetchone()
c.close()
output = open("data.html")
return output
if __name__ == '__main__':
cherrypy.config.update("app.conf")
cherrypy.quickstart(simple_db())
我的 html 文件,
<h1>Things to bring to our picnic</h1>
<head>
<style>
table {
border-collapse: collapse;
}
</style>
</head>
<table border=1>
<tr><th>Item</th><th>Quantity</th></tr>
% for row in data:
<tr>
% for col in row:
<td>{{col}}</td>
% endfor
</tr>
% endfor
【问题讨论】:
标签: python python-2.7 cherrypy