我试图打印出我存储在列表中的一些元组但是我一直在得到空白表格。以下是我的一些代码:
__init__.py
:
def view_jobs():
conn = sqlite3.connect(DBNAME)
c = conn.cursor()
c.execute('SELECT * FROM Companies, Job WHERE Companies.cid = Job.cid')
rows = c.fetchall()
conn.commit()
conn.close()
return rows
web_page.py
:
@app.route('/Student/Search', methods=['GET', 'POST'])
def studentSearch():
table = []
if request.method == 'POST':
return render_template('studentsearch.html')
else:
table = adb.view_jobs()
print table
return render_template('studentsearch.html', table=table)
studentsearch.html:
<tr>
<th>Company Name</th>
<th>Company E-mail</th>
<th>Internship Position</th>
<th>Internship Description</th>
</tr>
<tr>
{% for row in table %}
<td>{{row.name}}</td>
<td>{{row.email}}</td>
<td>{{row.job_type}}</td>
<td>{{row.job_description}}</td>
{% endfor %}
</tr>
当我打印到控制台时,我得到了相应的数据但是当我运行它时,我得到一个没有数据输出的站点。我有这个唯一的经验就是使用Django,当我将数组传递给HTML时,它似乎保留了属性名称,但我可能正在使用更复杂的东西来查询...不确定是否有我所缺少的与烧瓶有关的任何东西。
我认为错误在于我如何通过table
,但我不确定最好的方法是什么
答案 0 :(得分:2)
问题出在这里
<tr>
{% for row in table %}
<td>{{row.name}}</td> # You are trying to access a tuple attribute like a dictionary
<td>{{row.email}}</td> # Same here and below
<td>{{row.job_type}}</td>
<td>{{row.job_description}}</td>
{% endfor %}
</tr>
您可以做的更改是
转换查询:
c.execute('SELECT name, email, job_type, job_description FROM Companies, Job WHERE Companies.cid = Job.cid')
然后在studentsearch.html
更改
<tr>
{% for row in table %}
<td>{{row[0]}}</td>
<td>{{row[1]}}</td>
<td>{{row[2]}}</td>
<td>{{row[3]}}</td>
{% endfor %}
</tr>