Priya Sharma
Jan 2026
3 tabs
# Optional: custom error handlers
handler404 = 'myapp.views.custom_404'
handler500 = 'myapp.views.custom_500'
# In views.py
def custom_404(request, exception):
return render(request, '404.html', status=404)
def custom_500(request):
return render(request, '500.html', status=500)
{% extends "base.html" %}
{% block title %}Page Not Found{% endblock %}
{% block content %}
<div class="error-page">
<h1>404 - Page Not Found</h1>
<p>The page you're looking for doesn't exist.</p>
<a href="{% url 'home' %}" class="btn">Go Home</a>
</div>
{% endblock %}
<!DOCTYPE html>
<html>
<head>
<title>Server Error</title>
</head>
<body>
<h1>500 - Server Error</h1>
<p>Something went wrong. We're working on it!</p>
<a href="/">Go Home</a>
</body>
</html>
3 files · python, django
Explain with highlit
Custom error pages improve user experience and brand consistency. I create templates named 404.html, 500.html, 403.html, and 400.html in the templates root. Django serves these automatically when DEBUG=False. For custom logic, I can override error handler views in urls.py. I ensure error pages don't depend on database or cache to prevent cascading failures. For 500 errors, I keep templates simple. I test error pages in production-like settings. This provides better UX than default Django error pages.
Related snips
python
import os
import stat
for root, _dirs, files in os.walk('/etc'):
for name in files:
path = os.path.join(root, name)
Python security audit script for exposed risky filesystem state
python
auditing
host-security
by Kai Nakamura
1 tab
python
class Product(models.Model):
name = models.CharField(max_length=200)
slug = models.SlugField(blank=True)
price = models.DecimalField(max_digits=10, decimal_places=2)
cost = models.DecimalField(max_digits=10, decimal_places=2)
margin = models.DecimalField(max_digits=5, decimal_places=2, blank=True)
Django model signals vs overriding save
django
python
models
by Priya Sharma
2 tabs
python
from django.urls import path
from . import views
app_name = 'blog'
urlpatterns = [
Django URL namespacing and reverse lookups
django
python
urls
by Priya Sharma
3 tabs
python
import graphene
from graphene_django import DjangoObjectType
from blog.models import Post, Comment
class PostType(DjangoObjectType):
Django GraphQL with Graphene
django
python
graphql
by Priya Sharma
2 tabs
python
from django.db.models import Count, Avg, Sum, Q, F
from django.views.generic import TemplateView
from products.models import Product, Order, OrderItem
class DashboardView(TemplateView):
Django aggregation with annotate for statistics
django
python
database
by Priya Sharma
1 tab
python
from rest_framework import permissions
class IsOwner(permissions.BasePermission):
"""Allow only object owner to access."""
Django REST Framework permissions and authorization
django
python
rest
by Priya Sharma
2 tabs
Share this code
Here's the card — post it anywhere.