🎸 Django Beginner

What is Django's request/response cycle?

Why Interviewers Ask This

This question tests conceptual clarity. Interviewers want to hear a precise, confident definition before moving to more complex Django topics. It also reveals how well you can explain technical ideas to non-experts.

Answer

Django's request/response cycle processes every HTTP request through a defined pipeline: (1) Web server receives request: Nginx/Apache forwards to Django via WSGI (Gunicorn, uWSGI) or ASGI (Daphne, Uvicorn for async). (2) WSGI/ASGI handler: creates an HttpRequest object from the raw request with: method (GET/POST), path, headers, GET/POST/FILES data, session, user (via middleware), META. (3) Middleware (request phase): each middleware in MIDDLEWARE list processes the request top-to-bottom. SecurityMiddleware, SessionMiddleware, AuthenticationMiddleware add data to request. Any middleware can short-circuit by returning a response. (4) URL resolver: matches request.path against urlpatterns. Finds the matching view function/class. Extracts URL parameters. Raises 404 if no match. (5) View: receives request + URL parameters. Queries models, applies business logic. Returns an HttpResponse (or raises Http404, PermissionDenied, etc.). (6) Middleware (response phase): each middleware processes the response in reverse order (bottom-to-top). (7) Response sent to client: HttpResponse has status_code, headers, content. (8) HttpRequest attributes: request.method, request.GET (QueryDict), request.POST (QueryDict), request.FILES, request.user (AnonymousUser if not authenticated), request.session (dict-like), request.META (server vars: REMOTE_ADDR, HTTP_USER_AGENT), request.COOKIES, request.path, request.is_ajax() (deprecated — check header).

Common Mistake

Candidates often give textbook answers here. Interviewers are more impressed when you relate the concept to a specific problem you solved in a real Django project.