假设这是您正在关注的 Flask 教程,我认为您的函数缺少一些东西。是entries
某种全局变量,还是您只是删除了创建它的部分?我尝试将您的代码与教程中的一个示例结合起来,并添加了一些注释。
@app.route('/order', methods=['POST'])
def order_entry():
# the following line creates a 'cursor' which you need to retrieve data
# from the database
cur = g.db.execute('select title, text from entries order by id desc')
# the following line uses that cursor ("cur"), fetches the data,
# turns it into a (unsorted) list of dictionaries
entries = [dict(title=row[0], text=row[1]) for row in cur.fetchall()]
# let's sort the list by the 'title' attribute now
entries = sorted(entries, key=lambda d: d['title'])
# or if you prefer, you could say: "entries.sort(key=lambda d:d['title']"
# return the template with the sorted entries in
return render_template('show_entries.html', entries=entries)
现在,我根本不知道 Flask,但我认为这就是gist
你想要做的。
您可能需要阅读一些 Python 教程(在处理 Flask 之前),因为有一些基本概念,一旦您掌握,我认为会让其他一切变得更容易。