python / intermediate
Snippet
Django Middleware Customization and Request Processing
Django middleware processes requests/responses in a stacked pipeline. MiddlewareMixin provides compatibility between Django versions. process_request runs before view execution (return None to continue), while process_response runs after, allowing header modification. Middleware can short-circuit responses (returning HttpResponse stops the chain). Rate limiting via cache is a common pattern for API protection against abuse.
snippet.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import jsonfrom django.utils.deprecation import MiddlewareMixinfrom django.contrib.auth.models import Userfrom django.core.cache import cacheclass RequestLoggingMiddleware(MiddlewareMixin):def process_request(self, request):request.start_time = __import__('time').time()print(f'Request: {request.method} {request.path}')return Nonedef process_response(self, request, response):duration = __import__('time').time() - request.start_timeresponse['X-Response-Time'] = f'{duration:.3f}s'print(f'Completed in {duration:.3f}s - Status: {response.status_code}')return responseclass RateLimitMiddleware(MiddlewareMixin):def process_request(self, request):if request.method == 'POST':ip = request.META.get('REMOTE_ADDR')cache_key = f'rate_limit:{ip}'hits = cache.get(cache_key, 0)if hits >= 10:return JsonResponse({'error': 'Rate limit exceeded'}, status=429)cache.set(cache_key, hits + 1, 60)return None
django
Breakdown
1
def process_request(self, request):
Executes before view resolution; returning None continues to next middleware or view
2
def process_response(self, request, response):
Executes after view renders; must return the response object
3
cache.set(cache_key, hits + 1, 60)
Cache stores hit count per IP for 60 seconds for rate limiting
4
return JsonResponse({'error': 'Rate limit exceeded'}, status=429)
Short-circuits request chain by returning response directly