Python | 404 Error handling in Flask

Prerequisite: Creating simple application in Flask

A 404 Error is showed whenever a page is not found. Maybe the owner changed it’s URL and forgot to change the link or maybe they deleted the page itself. Every site needs a Custom Error page to avoid the user to see the default Ugly Error page.

GeeksforGeeks also has a customized error page. If we type a URL like

Default 404 Error

GeeksForGeeks Customized Error Page

It will show Error 404 page since this URL doesn’t exist. But an error page provides a beautiful layout, helps the user to go back or even takes them to the homepage after a specific time interval. That is why Custom Error pages are necessary for every website.

Flask provides us with a way to handle the error and return our Custom Error page.

For this, we need to download and import flask. Download flask through the following commands on CMD.

pip install flask

Using app.py as our Python file to manage templates, 404.html be the file we will return in the case of a 404 error and header.html be the file with header and navbar of a website.

Flask allows us to make a python file to define all routes and functions. In app.py we have defined the route to the main page (‘/’) and error handler function which is a flask function and we passed 404 error as a parameter.

from flask import Flask, render_template
app = Flask(__name__)
# app name
# 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.

The following code exports header and navbar from header.html.
Both files should be stored in templates folder according to the flask.

{% 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 after 5 seconds

The app.py code for this example stays same as above.
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.
The following code exports header and navbar from header.html.
Both files should be stored in templates folder according to the flask.
After 5 seconds, the user will get redirected to the Home Page Automatically.

<title>Page Not Found</title>
<script language="JavaScript" type="text/javascript">
var seconds =6;
// countdown timer. took 6 because page takes approx 1 sec to load
var url="{{url_for(index)}}";
// variable for index.html url
function redirect(){
 if (seconds <=0){
 // redirect to new url after counter  down.
  window.location = url;
 } else {
  document.getElementById("pageInfo").innerHTML="Redirecting to Home Page after "
+seconds+" seconds."
  setTimeout("redirect()", 1000)
{% extends "header.html" %}
//exporting navbar and header from header.html
{% block body %}
 <body onload="redirect()">
<p id="pageInfo"></p>
{% endblock %}

Sample header.html
This is a sample header.html which includes navbar just like shown in the image.
It’s made up of bootstrap. You can also make one of your own.
For this one, refer the bootstrap documentation.

<!DOCTYPE html>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/
3XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K
/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <!-- Starting header -->
    <nav class="navbar navbar-expand-lg navbar-light bg-light">
  <a class="navbar-brand" href="#">Navbar</a>
  <!-- bootstrap classes for navbar -->
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target=
"#navbarSupportedContent" aria-controls="navbarSupportedContent"
 aria-expanded="false"  aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  <div class="collapse navbar-collapse" id="navbarSupportedContent">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item active">
        <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
      <li class="nav-item">
        <a class="nav-link" href="#">Link</a>
      <li class="nav-item dropdown">
        <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown"
 role="button data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
        <div class="dropdown-menu" aria-labelledby="navbarDropdown">
          <a class="dropdown-item" href="#">Action</a>
          <a class="dropdown-item" href="#">Another action</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="#">Something else here</a>
      <li class="nav-item">
        <a class="nav-link disabled" href="#">Disabled</a>
    <form class="form-inline my-2 my-lg-0">
      <input class="form-control mr-sm-2" type="search"
       placeholder="Search" aria-label="Search">
      <button class="btn btn-outline-success my-2 my-sm-0" 
 <body >
  {%block body%}

The output will be a custom error page with header.html that user exported.
The following is an example output with my custom header, footer and 404.html file.

This article is attributed to GeeksforGeeks.org

leave a comment



load comments

Subscribe to Our Newsletter