Python 有人能帮我解决这个问题吗
我尝试了一个从互联网上获取的作业,用于将数据从烧瓶显示到hmtl表单。 但问题是我有两个错误:- 1) 找不到flask中呈现的HTML文件。 2) SQL表和列未解析 我曾尝试通过互联网用许多解决方案来解决这个问题。 我在Pycharm上试过,使用MariaDB连接器,测试连接成功。 有人能帮我解决吗 烧瓶代码如下:-Python 有人能帮我解决这个问题吗,python,html,mysql,database,flask,Python,Html,Mysql,Database,Flask,我尝试了一个从互联网上获取的作业,用于将数据从烧瓶显示到hmtl表单。 但问题是我有两个错误:- 1) 找不到flask中呈现的HTML文件。 2) SQL表和列未解析 我曾尝试通过互联网用许多解决方案来解决这个问题。 我在Pycharm上试过,使用MariaDB连接器,测试连接成功。 有人能帮我解决吗 烧瓶代码如下:- from flask import Flask, render_template, request from flaskext.mysql import MySQL app
from flask import Flask, render_template, request
from flaskext.mysql import MySQL
app = Flask(__name__)
# Database connection info
app.config['MYSQL_DATABASE_USER'] = 'root'
app.config['MYSQL_DATABASE_PASSWORD'] = ''
app.config['MYSQL_DATABASE_DB'] = 'LibraryDB'
app.config['MYSQL_DATABASE_HOST'] = 'localhost'
mysql = MySQL()
mysql.init_app(app)
conn = mysql.connect()
cursor = conn.cursor()
@app.route('/search', methods=['GET', 'POST'])
def search():
if request.method == "POST":
book = request.form['book']
# search by author or book
cursor.execute("SELECT name, author from book WHERE name LIKE %s OR author LIKE %s", (book,book))
conn.commit()
data = cursor.fetchall()
# all in the search box will return all the tuples
if len(data) == 0 and book == 'all':
cursor.execute("SELECT name, author from Book")
conn.commit()
data = cursor.fetchall()
return render_template('search.html', data=data)
return render_template('search.html')
if __name__ == '__main__':
app.debug = True
app.run()
<!DOCTYPE html>
<html lang="utf-8">
<head>
<link rel="stylesheet" href="../static/index.css" type="text/css">
<title>Search</title>
</head>
<body>
<h1 style="text-align: center;">Library</h1>
<form class="example" method="post" action=""
style="margin:auto;max-width:600px">
<label>
<input type="text" placeholder="Search by author or book, or all to see all the data" name="book">
</label>
<button type="submit">Search<i class="fa fa-search"></i>
</button>
</form>
<p></p>
<div style="text-align: center;">
{% for item in data %}
<tr>
<td> {{item[0]}} by {{item[1]}}</td>
</tr>
{% endfor %}
</div>
</body>
</html>
我的HTML代码如下:-
from flask import Flask, render_template, request
from flaskext.mysql import MySQL
app = Flask(__name__)
# Database connection info
app.config['MYSQL_DATABASE_USER'] = 'root'
app.config['MYSQL_DATABASE_PASSWORD'] = ''
app.config['MYSQL_DATABASE_DB'] = 'LibraryDB'
app.config['MYSQL_DATABASE_HOST'] = 'localhost'
mysql = MySQL()
mysql.init_app(app)
conn = mysql.connect()
cursor = conn.cursor()
@app.route('/search', methods=['GET', 'POST'])
def search():
if request.method == "POST":
book = request.form['book']
# search by author or book
cursor.execute("SELECT name, author from book WHERE name LIKE %s OR author LIKE %s", (book,book))
conn.commit()
data = cursor.fetchall()
# all in the search box will return all the tuples
if len(data) == 0 and book == 'all':
cursor.execute("SELECT name, author from Book")
conn.commit()
data = cursor.fetchall()
return render_template('search.html', data=data)
return render_template('search.html')
if __name__ == '__main__':
app.debug = True
app.run()
<!DOCTYPE html>
<html lang="utf-8">
<head>
<link rel="stylesheet" href="../static/index.css" type="text/css">
<title>Search</title>
</head>
<body>
<h1 style="text-align: center;">Library</h1>
<form class="example" method="post" action=""
style="margin:auto;max-width:600px">
<label>
<input type="text" placeholder="Search by author or book, or all to see all the data" name="book">
</label>
<button type="submit">Search<i class="fa fa-search"></i>
</button>
</form>
<p></p>
<div style="text-align: center;">
{% for item in data %}
<tr>
<td> {{item[0]}} by {{item[1]}}</td>
</tr>
{% endfor %}
</div>
</body>
</html>
搜索
图书馆
搜索
{数据%中的项的%1}
{{item[0]}}通过{{item[1]}}
{%endfor%}
我对CSS很在行,所以我们不必担心,您是否检查了数据是否被解析到查询中,以及它是否从表中获取结果?您可以使用以下代码进行检查:
print(cursor._executed)
在cursor.execute()之后
对于错误号1:
我会说,请验证HTML文件的名称
p、 s无需使用conn.commit()
,因为建议仅在创建记录、更新记录或从表中删除记录时使用,因此您可以继续删除该部分
让我知道这是否对您有帮助。您需要指定准确的错误,如发生错误的行或共享屏幕截图