Explain Django's MTV architecture and how a request flows through it.
Django calls its pattern MTV — Model, Template, View — which maps onto MVC with the names shifted. Django's View is MVC's controller, and Django's Template is MVC's view. The framework itself plays the controller role.
The flow of a request:
- The WSGI or ASGI server hands the request to Django, which wraps it in an
HttpRequest. - Middleware runs top to bottom. Each layer can inspect or modify the request, or short-circuit it entirely — this is where sessions, authentication, and CSRF checks happen.
- URL resolution matches the path against
urlpatternsand extracts any captured arguments. - The view runs. It talks to models through the ORM, applies business logic, and returns an
HttpResponse. - If it rendered a template, the template engine produces the HTML.
- Middleware runs again in reverse order on the way out, then the response is returned.
Note: The reverse ordering of middleware on the response is a common follow-up. It matters because a middleware that sets a header on the way out sees the response after everything registered below it.





