Skip to content

How to Fix Flask 404 Not Found Error

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about How to Fix Flask 404 Not Found Error. We cover key concepts, practical examples, and best practices.

Flask returns a 404 Not Found error when the requested URL does not match any registered route in the application.

The Wrong Way

from flask import Flask

app = Flask(__name__)

@app.route('/home')
def home():
    return "Hello"

if __name__ == '__main__':
    app.run()

Visiting http://127.0.0.1:5000/ returns:

404 Not Found

The Right Way

Define a route for the root URL and the intended paths:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
    return "Home Page"

@app.route('/home')
def home():
    return "Hello"

if __name__ == '__main__':
    app.run()

Visiting http://127.0.0.1:5000/ now returns:

Home Page

Step-by-Step Fix

1. List all registered routes

with app.test_request_context():
    print(app.url_map)

This shows all routes the application knows about.

2. Check blueprint registration

from flask import Blueprint

api = Blueprint('api', __name__)

@api.route('/users')
def get_users():
    return "User list"

app = Flask(__name__)
app.register_blueprint(api, url_prefix='/api')

# Now /api/users works, but /users does not

3. Use strict_slashes carefully

# These two routes are different with strict_slashes enabled
@app.route('/about')    # matches /about (404 on /about/)
@app.route('/about/')   # matches /about/ (redirects to /about)

4. Add a catch-all 404 handler

@app.errorhandler(404)
def not_found(e):
    return {"error": "Page not found"}, 404

Prevention Tips

  • List all routes with app.url_map during development.
  • Use blueprints with clear url_prefix values.
  • Test URLs with app.test_client() before deployment.
  • Serve static files through Flask or a reverse proxy explicitly.
  • Use url_for() to generate URLs instead of hardcoding them.

Common Mistakes with flask 404

  1. Misunderstanding that String is [Char] with poor performance for large text operations
  2. Using foldl instead of foldl' causing stack overflow on large lists
  3. Forgetting deriving (Show, Eq) on custom data types needed for debugging

These mistakes appear frequently in real-world PYTHON code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### Why does Flask return 404 for a file that exists in the static folder?

Static files are served under the /static/ URL path by default. Access them as /static/filename.css. If you moved the static folder, configure it with app.static_folder = 'custom_path'.

Can a missing trailing slash cause a 404?

By default, Flask redirects /about/ to /about (without trailing slash). If you defined only /about/ and visit /about, you get a 404. Use strict_slashes=False on the route.

How do I serve a single-page app catch-all route?

Add a catch-all route at the end of your route definitions: @app.route('/<path:path>') that returns the SPA index.html. This must be the last route because it matches everything.

Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro