运行Python与Flask应用时遭遇404 Not Found错误求助
Hey there! Let's break down the issues causing that 404 error and other bugs in your Flask app step by step:
1. Missing Root Route (The 404 Culprit)
Right now, your app only has a /search endpoint, but you're trying to load the homepage (the root URL /). Flask can't find a matching route for /, so it throws the "Not Found" error.
Fix:
Add a root route that redirects to your search page, or adjust your existing route to handle the root URL. Here's the redirect approach:
@app.route('/') def home(): # Send users straight to the search page when they visit the root return redirect('/search')
Alternatively, you can make your search route handle both / and /search:
@app.route('/', methods=['GET', 'POST']) @app.route('/search', methods=['GET', 'POST']) def search(): # Keep your existing search logic here
2. Mismatched Form Field Name
In your Search.html, the input field uses name="book", but in your Python code you're trying to fetch request.form['fn'] — this will throw a KeyError when someone submits the form.
Fix:
Pick one name and stick with it. Either update the HTML:
<input type="text" placeholder="Search by author or book, or all to see all the data" name="fn">
Or update the Python code:
fn = request.form['book']
3. SQL Query Mistakes
Your cursor.execute line has an extra fn parameter, and you're missing wildcards for the LIKE clause (so it'll only match exact flight numbers). Also, you don't need conn.commit() for SELECT queries — that's only for write operations like INSERT/UPDATE/DELETE.
Fix:
# Add % wildcards to match partial strings, fix parameter count, and remove unnecessary commit cursor.execute("SELECT origin, flightNumbers from flightinfo WHERE flightNumbers LIKE %s ", (f"%{fn}%",)) # Remove conn.commit() here and after the all-data query
4. Invalid HTML Structure
You're using <tr> and <td> tags outside of a <table> element, which is invalid HTML and will cause weird rendering issues.
Fix:
Wrap your loop content in a table:
<center> <table> {% for item in data %} <tr> <td> {{item[0]}} by {{item[1]}}</td> </tr> {% endfor %} </table> </center>
5. Database Connection Best Practice
Using a global cursor/connection can cause problems with concurrent requests. It's better to create and close connections inside your route function.
Fix:
Rewrite your search route to handle connections locally:
@app.route('/search', methods=['GET', 'POST']) def search(): conn = mysql.connect() cursor = conn.cursor() data = [] if request.method == "POST": fn = request.form['book'] # Or 'fn', depending on your earlier fix if fn == 'all': cursor.execute("SELECT origin, flightNumbers from flightinfo") else: cursor.execute("SELECT origin, flightNumbers from flightinfo WHERE flightNumbers LIKE %s ", (f"%{fn}%",)) data = cursor.fetchall() cursor.close() conn.close() return render_template('search.html', data=data)
Putting all these fixes together should resolve the 404 error and the other bugs in your app. Let me know if you hit any snags!
内容的提问来源于stack exchange,提问作者Adam Wolarczuk




