运行 python 和 Flask 应用程序时找不到 404
Getting a 404 not found when running a python and flask app
大家晚上好,第一次在这里工作,我正在开发一个 python 和 Flask 应用程序,无论我做了什么更改,当我加载第一页 Not Found 时,我总是收到错误消息
在服务器上找不到请求的 URL。如果您手动输入了 URL,请检查您的拼写并重试。我确定我正在做的事情很小,我有阅读障碍,所以 10 次中有 9 次是我没有看到的事情
这是Search.html
<!-- Search Bar -->
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-
scale=1">
<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-
awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="/static/index.css">
</head>
<body>
<h1><center>Library</center></h1>
<form class="example" method="post" action=""
style="margin:auto;max-width:600px">
<input type="text" placeholder="Search by author or
book, or all to see all the data" name="book">
<button type="submit"><i class="fa fa-search"></i>
</button>
</form>
<p></p>
<center>
{% for item in data %}
<tr>
<td> {{item[0]}} by {{item[1]}}</td>
</br>
</tr>
{% endfor %}
</center>
</body>
</html>
这是libary.py
# library.py
from flask import Flask, render_template, request, redirect
from flaskext.mysql import MySQL
app = Flask(__name__)
# Database connection info. Note that this is not a secure connection.
app.config['MYSQL_DATABASE_USER'] = 'root'
app.config['MYSQL_DATABASE_PASSWORD'] = '*******'
app.config['MYSQL_DATABASE_DB'] = 'flightdata'
app.config['MYSQL_DATABASE_HOST'] = 'localhost'
mysql = MySQL()
mysql.init_app(app)
conn = mysql.connect()
cursor = conn.cursor()
#endpoint for search
@app.route('/search', methods=['GET', 'POST'])
def search():
if request.method == "POST":
fn = request.form['fn']
# search by Flight Number
cursor.execute("SELECT origin, flightNumbers from flightinfo WHERE flightNumbers LIKE %s ", (fn , fn))
conn.commit()
data = cursor.fetchall()
if len(data) == 0 and fn == 'all':
cursor.execute("SELECT origin, flightNumbers from flightinfo")
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()
提前感谢您的帮助
根据评论,您输入的 URL 是错误的,因为您添加了路线:
@app.route('/search', methods=['GET', 'POST'])
你应该输入 URL 127.0.0.1:5000/search
大家晚上好,第一次在这里工作,我正在开发一个 python 和 Flask 应用程序,无论我做了什么更改,当我加载第一页 Not Found 时,我总是收到错误消息 在服务器上找不到请求的 URL。如果您手动输入了 URL,请检查您的拼写并重试。我确定我正在做的事情很小,我有阅读障碍,所以 10 次中有 9 次是我没有看到的事情
这是Search.html
<!-- Search Bar -->
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-
scale=1">
<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-
awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="/static/index.css">
</head>
<body>
<h1><center>Library</center></h1>
<form class="example" method="post" action=""
style="margin:auto;max-width:600px">
<input type="text" placeholder="Search by author or
book, or all to see all the data" name="book">
<button type="submit"><i class="fa fa-search"></i>
</button>
</form>
<p></p>
<center>
{% for item in data %}
<tr>
<td> {{item[0]}} by {{item[1]}}</td>
</br>
</tr>
{% endfor %}
</center>
</body>
</html>
这是libary.py
# library.py
from flask import Flask, render_template, request, redirect
from flaskext.mysql import MySQL
app = Flask(__name__)
# Database connection info. Note that this is not a secure connection.
app.config['MYSQL_DATABASE_USER'] = 'root'
app.config['MYSQL_DATABASE_PASSWORD'] = '*******'
app.config['MYSQL_DATABASE_DB'] = 'flightdata'
app.config['MYSQL_DATABASE_HOST'] = 'localhost'
mysql = MySQL()
mysql.init_app(app)
conn = mysql.connect()
cursor = conn.cursor()
#endpoint for search
@app.route('/search', methods=['GET', 'POST'])
def search():
if request.method == "POST":
fn = request.form['fn']
# search by Flight Number
cursor.execute("SELECT origin, flightNumbers from flightinfo WHERE flightNumbers LIKE %s ", (fn , fn))
conn.commit()
data = cursor.fetchall()
if len(data) == 0 and fn == 'all':
cursor.execute("SELECT origin, flightNumbers from flightinfo")
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()
提前感谢您的帮助
根据评论,您输入的 URL 是错误的,因为您添加了路线:
@app.route('/search', methods=['GET', 'POST'])
你应该输入 URL 127.0.0.1:5000/search