gpt4 book ai didi

python - 为什么我的 MySQL 数据没有显示到 html 模板?

转载 作者:行者123 更新时间:2023-12-01 06:49:14 24 4
gpt4 key购买 nike

我一直在尝试显示来自 MySQL 数据库的数据,但没有任何效果。这是代码:

@app.route('/',methods=['GET','POST'])
def index():
if request.method == "POST":
food = request.form['food']
cur = mysql.connection.cursor()
data = cur.execute("SELECT * FROM recipies WHERE title = %s",[food])
if data > 0 :
results = cur.fetchall()
return render_template('results.html',results=results)

和 html

% extends "base.html" %}
{% block content %}

{% for result in results %}
<table>
<td>{{ result }}</td>
</table>

{% endfor %}


{% endblock %}

当我运行它时,它给出一个错误“TypeError: View 函数没有返回有效的响应。该函数要么返回 None,要么在没有 return 语句的情况下结束。”我不知道该怎么办。

最佳答案

对您的函数进行以下更改:

@app.route('/',methods=['GET','POST'])
def index():
if request.method == "POST":
food = request.form['food']
cur = mysql.connection.cursor()
cur.execute("SELECT * FROM recipies WHERE title = %s",[food])
results = []
if not cur.rowcount:
print "No results found"
else:
results = cur.fetchall()
return render_template('results.html',results=results)

关于python - 为什么我的 MySQL 数据没有显示到 html 模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59100166/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com