Django: The Request-Response Cycle
February 18, 2025
1 min read
0 views
When you type google.com and hit Enter, what actually happens?
The Journey
- Browser: Resolves DNS to an IP.
- Nginx: Receives the request on port 80/443. It acts as a Reverse Proxy.
- Gunicorn: Nginx passes the request to Gunicorn (WSGI Server) via a Unix Socket.
- Django WSGI: Gunicorn calls Django's
applicationcallable. - Middleware: The request flows through layers (Security, Session, CSRF).
- URL Dispatcher: Django looks at
urls.py. - View: The logic runs. Database is queried.
- Template: HTML is rendered.
Middleware: The Onion
Think of Django as an onion. The request has to go through all layers to reach the center (the View), and the response goes back out through them.
Python
# Custom Middleware Check
def simple_middleware(get_response):
def middleware(request):
# Code executed BEFORE the view
print("Request coming in!")
response = get_response(request)
# Code executed AFTER the view
print("Response going out!")
return response
return middleware
Conclusion
Understanding this cycle is crucial for debugging. If a request never reaches your View, it's likely stuck in Middleware or blocked by Nginx.
Similar Posts
Security: JWT vs Sessions
Oct 22, 2025
Django: Signals vs Overriding Save
Sep 14, 2025
System Design: Caching Strategies
Aug 20, 2025