Django

Create and Configure a Custom 404 Error Page

🎯 Objective

To create a custom 404 error page when a user visits a non-existing URL.


❗ What is 404 Error?

404 error occurs when:

  • Page not found
  • Invalid URL entered

📄 Step 1: Create Template

Create file:

templates/404.html
<!DOCTYPE html>
<html>
<head>
<title>Page Not Found</title>
</head>
<body>
<h1>404 Error</h1>
<p>Oops! Page not found.</p>
<a href="/">Go to Home</a>
</body>
</html>

⚙️ Step 2: Configure Settings

# settings.py
DEBUG = False
ALLOWED_HOSTS = ['*']

🔗 Step 3: Add Handler in URLs

# project urls.py
handler404 = 'yourapp.views.custom_404'

🧠 Step 4: Create View

# views.py
from django.shortcuts import render

def custom_404(request, exception):
return render(request, '404.html', status=404)

🚀 Step 5: Test It

  • Run server
  • Open invalid URL:
http://127.0.0.1:8000/xyz/

⚠️ Important Note

  • Custom 404 works only when DEBUG = False
  • Restart server after changes

🧠 Exam Tips

  • 404 = Page Not Found
  • Must define handler404
  • Template name should be 404.html
  • Requires DEBUG = False

🔥 Viva Questions

  1. What is 404 error?
  2. Why custom error pages are used?
  3. What is handler404?
  4. When does Django show default 404 page?
  5. Why DEBUG must be False?

Further Reading

Introduction to Django Framework and its Features

Django Practice Exercise

Examples of Array Functions in PHP

Basic Programs in PHP

Registration Form Using PDO in PHP

Inserting Information from Multiple CheckBox Selection in a Database Table in PHP

programmingempire

princites.com

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *