Performance: Process request and process response
This affects how quickly the server handles incoming requests and sends back responses, impacting page load speed and interaction responsiveness.
Jump into concepts and practice - no test required
import asyncio def middleware(get_response): async def middleware_func(request): # Offload heavy task asynchronously asyncio.create_task(do_heavy_task_async()) response = await get_response(request) # Lightweight response modification response.content = fast_modification(response.content) return response return middleware_func
def middleware(get_response): def middleware_func(request): # Heavy synchronous processing before response do_heavy_task() response = get_response(request) # Modifying response with expensive operations response.content = expensive_modification(response.content) return response return middleware_func
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Synchronous heavy processing in middleware | N/A (server-side) | N/A | Delays browser paint start | [X] Bad |
| Asynchronous lightweight processing in middleware | N/A (server-side) | N/A | Faster browser paint start | [OK] Good |
request object in a Django view primarily contain?from django.http import JsonResponse
def my_view(request):
data = {'name': 'Alice', 'age': 30}
return JsonResponse(data)from django.http import HttpResponse
def bad_view(request):
response = HttpResponse('Hello')
response.status_code = '404'
return response