好的,我现在已经尝试了近10个小时了,我无法弄明白。我有一个餐厅搜索应用程序,在将输出打印到终端时工作,但我不知道如何让它在新页面上打印。这就是我到目前为止所做的:
@app.route('/')
def results(area, rating, food):
conn = sqlite3.connect('SRG.db')
c = conn.cursor()
c.execute('SELECT * FROM restaurants WHERE address LIKE "%%%s%%" \
AND rating > %s-1 AND type LIKE "%%%s%%" ORDER BY rating DESC' \
% (area, rating, food,))
while True:
row = c.fetchone()
if row == None:
break
print 'Name: ',row[1]
print 'Rating: ',row[3]
print 'Price: ',row[2]
print 'Type: ',row[4]
print 'Website: ',row[5]
print 'Address: ',row[6]
print 'Subway: ',row[7]
print 'Phone: ',row[8],'\n'
return render_template('results.html', row=row)
现在,该部分工作并打印到终端。我将它打印到html页面时遇到问题。这是我到目前为止的模板:
<head>
<meta charset="UTF-8">
<title>Here are your results</title>
</head>
<body>
<h1>Check out these restaurants</h1>
<ul>
{% for row in results %}
<p>Name: {{ row[1] }}</p>
<p>Rating: {{ row[3] }}</p>
<p>Price: {{ row[2] }}</p>
<p>Type: {{ row[4] }}</p>
<p>Website: {{ row[5] }}</p>
<p>Address: {{ row[6] }}</p>
<p>Subway: {{ row[7] }}</p>
<p>Phone: {{ row[8] }}</p>
{% endfor %}
<p><em>Sorry, no restaurants fit that criteria</em></p>
</ul>
</body>
</html>
有人可以帮忙吗?我觉得这是次要的,它让我发疯!这将是有史以来最好的圣诞礼物!
答案 0 :(得分:1)
尝试使用以下代码替换while循环下的代码:
results = c.fetchall()
return render_template('results.html', results=results)
两个修正:
results
变量,但您从未将其传递给render_template
函数。return
值。