A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python-404-error-handling-in-flask/ below:

Handling 404 Error in Flask

Handling 404 Error in Flask

Last Updated : 11 Jul, 2025

A 404 Error occurs when a page is not found. This can happen due to several reasons:

To improve user experience, websites should have a custom error page instead of showing a generic, unappealing one. GeeksforGeeks also uses a custom error page. For example, if we visit www.geeksforgeeks.org/ajneawnewiaiowjf, we'll see it in action.

Default 404 Error GeeksForGeeks Customized Error Page GFG Custom Error page

A custom error page improves user experience by providing a clean layout, navigation options, or auto-redirecting to the homepage. Flask lets us handle errors and display a custom page easily.

Before diving further in to the topic, make sure to Install and set up Flask

We will create app.py to manage templates, 404.html to handle 404 errors, and header.html for the website's header and navbar. Let's look at their code one by one:

app.py

Flask allows defining routes and functions in a Python file so create an app.py file for our main flask app. We set up the main page route ('/') and a 404 error handler using Flask's built-in function.

PYTHON
from flask import Flask, render_template

app = Flask(__name__)

# app name
@app.errorhandler(404)

# inbuilt function which takes error as parameter
def not_found(e):

# defining function
  return render_template("404.html")

The above python program will return 404.html file whenever the user opens a broken link.

header.html

The header.html file is a reusable template that contains the navbar and common page structure. It helps keep the code clean by avoiding repetition and makes updates easier. Other pages, like 404.html, extend it to maintain a consistent layout.

HTML
{% extends "header.html" %}
<!-- Exports header and navbar from header.html
     or any file you want-->
{% block title %}Page Not Found{% endblock %}
{% block body %}

  <h1>Oops! Looks like the page doesn't exist anymore</h1>
  <a href="{{ url_for('index') }}"><p>Click Here</a>To go to the Home Page</p>

<!-- {{ url_for('index') }} is a var which returns url of index.html-->
{% endblock %}
Automatically Redirecting to the Home page

The app.py code for this example stays the same as above. The following code Shows the Custom 404 Error page and starts a countdown of 5 seconds. After 5 seconds are completed, it redirects the user back to the homepage.

404.html

Following code exports header and navbar from header.html. Both files should be stored in the templates. After 5 seconds, the user will get redirected to the Home Page Automatically.

HTML
{% extends "header.html" %}

{% block title %}Page Not Found{% endblock %}

{% block body %}
    <h1>Oops! Looks like the page doesn't exist anymore</h1>
    <p id="pageInfo">Redirecting to Home Page after 5 seconds...</p>
    <script>
        var seconds = 5;
        var url = "{{ url_for('index') }}";

        function redirect() {
            if (seconds <= 0) {
                window.location = url;
            } else {
                seconds--;
                document.getElementById("pageInfo").innerHTML = "Redirecting to Home Page after " + seconds + " seconds.";
                setTimeout(redirect, 1000);
            }
        }
        window.onload = redirect;
    </script>
{% endblock %}
Output

Let's run the app using command - "python app.py", it will rener the home page which will look like this -

Home page

Now let's try to visit some random URL "http://127.0.0.1:5000/random_url", the custom error page will look like this -

Custom error page

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4