Python 从flask调用的sqlite db只返回变量,不返回值
我有一个flask应用程序,可以查询sqlite数据库:Python 从flask调用的sqlite db只返回变量,不返回值,python,html,sqlite,flask,Python,Html,Sqlite,Flask,我有一个flask应用程序,可以查询sqlite数据库: @app.route('/<subject_id>') def subject_id_lookup(subject_id): entries = query_db('select visitdt, cvnotes from exam where id = ?', [subject_id], one=True) return render_template('sho
@app.route('/<subject_id>')
def subject_id_lookup(subject_id):
entries = query_db('select visitdt, cvnotes from exam where id = ?',
[subject_id], one=True)
return render_template('show_results.html', entries = entries)
最后是我的show_results.html文件:
{% extends "layout.html" %}
{% block body %}
<ul class=entries>
{% for entry in entries %}
<li><h2>{{ entry }}</h2>
<br>
{% else %}
<li><em>No entry here</em>
{% endfor %}
</ul>
{% endblock %}
{%extends“layout.html”%}
{%block body%}
{entries%%中的条目的百分比}
- {{entry}}
{%else%}
- 这里不准入内
{%endfor%}
{%endblock%}
查询运行正常,但除了变量名
visitdt
和cvnotes
之外,什么也不返回。当我将上面的行更改为{{entry.cvnotes}}
时,它不返回任何内容。如何修改查询以显示我的subject\u id\u lookup()
函数的结果?问题在于query\u db
根据您指定的是one=True
还是one=False
返回不同的内容
>>> query_db(your_query, [some_id], one=True)
{visittd: "a value", cvnotes: "some notes"}
>>> query_db(your_query, [some_id], one=False)
[{visittd: "a value", cvnotes: "some notes"}] # Note the wrapping list
当您在字典上枚举时,结果是字典中的键-当您在列表上枚举时,结果是列表中的条目
>>> for thing in query_db(your_query, [some_id], one=True):
... print thing
visitdt
cvnotes
>>> for thing in query_db(your_query, [some_id], one=False):
... print thing
{visittd: "a value", cvnotes: "some notes"}
如果您想使用相同的模板,并且知道一个id
只返回一个值(或者如果您可以处理多个值),只需删除subject\u id\u lookup
中的one=True
关键字参数即可<代码>条目将是一个包含键的字典列表visitdt
和cvnotes
-当您在模板中对其进行迭代时,每个条目将成为一个结果字典(而不是单个结果字典中的键),并且{{entry.cvnotes}
将起作用
>>> for thing in query_db(your_query, [some_id], one=True):
... print thing
visitdt
cvnotes
>>> for thing in query_db(your_query, [some_id], one=False):
... print thing
{visittd: "a value", cvnotes: "some notes"}