python / beginner
Snippet
Basic Custom Middleware
Middleware is a framework of hooks into Django's request/response processing. It’s a light, low-level plugin system for globally altering input or output.
snippet.py
python
1
2
3
4
5
6
7
8
class SimpleLogMiddleware:def __init__(self, get_response):self.get_response = get_responsedef __call__(self, request):print(f"Request path: {request.path}")response = self.get_response(request)return response
django
Breakdown
1
self.get_response = get_response
Stores the next middleware or view in the chain to be called later.
2
def __call__(self, request):
The method executed for every request, allowing logic to run before and after the view.