Django 404 (page not found) Customize

engrmahabub
Mahabubur Rahman
Published on Sep, 11 2023 2 min read 0 comments
image

404 Page Not Found

If any one try to access a page that not exists in this web application, the application show a 404 Page not found( a 404 error page). Usually Django show a build-in template, that handles 404 errors . 

In this article you will learn how to customize this 404 view from templating, but first, just try to request a page that does not exist.

So in the browser window, type some  in the address bar URL that not exist in your application.

You will see one of bellow two  result : 

OR

 


If you got the first result, then DEBUG is set to True in your settings, and you must set it to False to get directed to the 404 template.

If you got the second result, you got directed to the built-in Django 404 template, because you set DEBUG to False.

This is done in the settings.py file of project directory.

 

Customize the 404 Template

 

Django look for a file named 404.html in the templates folder, and display it when there is a 404 error.

If no such file exists, Django shows the "Not Found" build-in template, that you saw in the example above.

To customize this message, all you have to do is to create a file in the templates folder root and name it 404.html, and design it whatever you want:

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>404 not found</title>
</head>
<body>
<h1>404!</h1>

<h2>Page Not Found !</h2>

<button>Go to Home</button>
</body>
</html>

Now try to access a page that not exist in this application, so you will see the customized 404 not found page as bellow :

 


 

 

0 Comments