🎯 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
- What is 404 error?
- Why custom error pages are used?
- What is handler404?
- When does Django show default 404 page?
- Why DEBUG must be False?
Further Reading
Introduction to Django Framework and its Features
Examples of Array Functions in PHP
Registration Form Using PDO in PHP
Inserting Information from Multiple CheckBox Selection in a Database Table in PHP
- Angular
- ASP.NET
- C
- C#
- C++
- CSS
- Dot Net Framework
- HTML
- IoT
- Java
- JavaScript
- Kotlin
- PHP
- Power Bi
- Python
- Scratch 3.0
- TypeScript
- VB.NET
