Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

Django – Creating a 404 Error Page

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

The 404 error is raised when the URL called upon doesn’t exist or has not been defined yet. This is commonly referred to as Page does not exist error. To handle requests from the undefined URLs in a website an error page is created. If these types of URLs are requested by the user then this error page gets executed to reflect the user that the page doesn’t exist and you can further redirect them to another URL like the Homepage. Using this technique we can also improve the SEO of our website.

In this article, we will create a simple 404 error page for our application.


Step 1 :  Go to file of your application and change this

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False


Step 2:  Go to the file of your application and put the below code:


from django.contrib import admin
from django.urls import include, path
# add the following path in the
# url patterns
urlpatterns = [
    path('', include('pages.urls'))
# add a flag for
# handling the 404 error
handler404 = 'pages.views.error_404_view'

Step 3: After that go to open up the  of your app and add the below code: 


from django.conf import settings
from django.shortcuts import redirect
def error_404_view(request, exception):
    # we add the path to the 404.html file
    # here. The name of our HTML file is 404.html
    return render(request, '404.html')

Step 4: Now just need to add the HTML page to your template page and your work is finished.


{% load static %}
<!DOCTYPE html>
<!--[if IE 8 ]>
<html class="no-js oldie ie8" lang="en">
   <!--[if IE 9 ]>
   <html class="no-js oldie ie9" lang="en">
      <!--[if (gte IE 9)|!(IE)]><!-->
      <html class="no-js" lang="en">
            <!--- basic page needs
               ================================================== -->
            <meta charset="utf-8">
            <title>Web Developer</title>
            <link href="{% static 'img/Demo/favicon.png' %}" rel="shortcut icon"/>
            <meta charset="UTF-8" />
            <meta http-equiv="X-UA-Compatible" content="IE=edge">
            <meta name="description" content="Alphaandroid - Creative Agency of web developers">
            <meta name="author" content="Pavan And Romil">
            <meta name="keywords" content="Web developer (using coding), Digital Marketing" />
            <!-- mobile specific metas
               ================================================== -->
            <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
            <!-- CSS
               ================================================== -->
            <link rel="stylesheet" href="{% static 'css/error404/base.css' %}">
            <link rel="stylesheet" href="{% static 'css/error404/main.css' %}">
            <link rel="stylesheet" href="{% static 'css/error404/vendor.css' %}">
            <!-- script
               ================================================== -->
            <script src="{% static 'js/error404/modernizr.js' %}"></script>
            <!-- favicons
               ================================================== -->
            <link rel="icon" type="image/png" href="{% static 'favicon.png' %}">
            <!-- Google Tag Manager -->
               new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
            <!-- End Google Tag Manager -->
            <!-- Google Tag Manager (noscript) -->
            <noscript><iframe src=""
               height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
            <!-- End Google Tag Manager (noscript) -->
            <!-- header
               ================================================== -->
            {% comment %}
            <header class="main-header">
               <div class="row">
                  <div class="logo">
                     <a href="index.html">Alpha Android</a>
               <a class="menu-toggle" href="#"><span>Menu</span></a>   
            <!-- /header --> {% endcomment %}
            <!-- navigation
               ================================================== -->
            {% comment %}
            <nav id="menu-nav-wrap">
               <h5>Site Pages</h5>
               <ul class="nav-list">
                  <li><a href="/" title="">Home</a></li>
                  <li><a href="#" title="">About</a></li>
                  <li><a href="#" title="">Contact</a></li>
               <h5>Some Text</h5>
<p>Lorem ipsum Non non Duis adipisicing pariatur eu enim Ut in aliqua dolor esse sed est in sit exercitation eiusmod aliquip consequat.</p>
            {% endcomment %}
            <!-- main content
               ================================================== -->
            <main id="main-404-content" class="main-content-particle-js">
               <div class="content-wrap">
                  <div class="shadow-overlay"></div>
                  <div class="main-content">
                     <div class="row">
                        <div class="col-twelve">
                           <h1 class="kern-this">404 Error.</h1>
                              Oooooops! Looks like nothing was found at this location.
                              Maybe try one of the links below, click on the top menu
                              or try a search?
                           {% comment %}
                           <div class="search">
                                 <input type="text" id="s" name="s" class="search-field" placeholder="Type and hit enter …">
                           {% endcomment %}
                        <!-- /twelve -->                       
                     <!-- /row -->                    
                  <!-- /main-content -->
                     <div class="row">
                        <div class="col-seven tab-full social-links pull-right">
                              <li><a href="#"><i class="fa fa-facebook"></i></a></li>
                              <li><a href="#"><i class="fa fa-behance"></i></a></li>
                              <li><a href="#"><i class="fa fa-twitter"></i></a></li>
                              <li><a href="#"><i class="fa fa-dribbble"></i></a></li>
                              <li><a href="#"><i class="fa fa-instagram"></i></a></li>
                        <div class="col-five tab-full bottom-links">
                           <ul class="links">
                              <li><a href="/">Homepage</a></li>
                              <li><a href="/about-us/">About Us</a></li>
                              {% comment %}
                              <li><a href="/contact-us/">Contact Us</a></li>
                              {% endcomment %}
                              <li><a href="">Report Error</a></li>
                           <div class="credits">
<p>Design by <a href="" title="styleshout"></a></p>
                     <!-- /row -->                     
               <!-- /content-wrap -->
            <!-- /main-404-content -->
            <div id="preloader">
               <div id="loader"></div>
            <!-- Java Script
               ================================================== -->
            <script src="{% static 'js/error404/jquery-2.1.3.min.js' %}"></script>
            <script src="{% static 'js/error404/plugins.js' %}"></script>
            <script src="{% static 'js/error404/main.js' %}"></script>

Step 5: Lastly whenever the 404 error is raised it will show them the 404.html file.:


Note: You will be able to see the output of this page only and only when you have made that website live on hosting or when you will set debug mode “False” and set your allowed host.

My Personal Notes arrow_drop_up
Last Updated : 28 Mar, 2023
Like Article
Save Article
Similar Reads
Related Tutorials