Flask应用部署后无法访问localhost:5000问题求助
Let’s work through why your Flask app is throwing a "Not Found" error even though the server seems to be running. Here are practical steps to diagnose and fix the issue:
Possible Causes & Fixes
Confirm your code is saved and executing correctly
Therunfileoutput shows it’s loadingflask_1.py, but IDEs like Spyder sometimes fail to pick up unsaved changes. Double-check that your code is saved, and ensure the@app.route('/')decorator is properly attached to theindex()function—no typos or extra spaces in the route string. Your code looks correct on paper, but a missing save could mean the server is running an older version of the file.Verify the server is running on port 5000
Flask defaults to port 5000, but if that port is already in use, it will automatically switch to another (like 5001). Look closely at your server output: does it explicitly state* Running on http://0.0.0.0:5000/? If it’s using a different port, update your URL to match (e.g.,http://localhost:5001/).Ensure you’re accessing the exact root route
Your route is set to/, so try accessing bothhttp://localhost:5000/andhttp://localhost:5000(the trailing slash is optional in Flask, but some browsers or proxies can mangle requests). Test in incognito mode to rule out cache or browser extension interference.Run the app directly from the terminal
IDE-specific run commands likerunfilecan sometimes introduce unexpected behavior. Open a command prompt, navigate to your project directory:cd C:/Users/RB287JD/Documents/ProgramsThen run the script directly:
python flask_1.pyThis bypasses any IDE quirks and ensures your code is loaded as intended.
Check for route conflicts
Make sure there’s no other code inflask_1.pythat overrides the/route later. If you have another@app.route('/')decorator on a different function, it will replace yourindex()route, leading to a 404 if that function isn’t working as expected.
内容的提问来源于stack exchange,提问作者dhinar1991

