What is Django middleware?
Middleware is a request and response processing layer that runs around the view.
- Runs in order on request
- Runs reverse order on response
- Good for auth, logging, headers
Quick recall
Middleware is a request and response processing layer that runs around the view.
QuerySets are lazy, so they usually do not hit the database until they are evaluated.
Regular Django views often render HTML, while API-style views return structured data like JSON for clients or frontend apps.
The messages framework stores one-time feedback like success, warning, or error notices across a redirect.
`get()` expects exactly one row and raises errors otherwise, while `filter()` returns a QuerySet that can contain zero or more rows.
Django forms combine HTML rendering, input parsing, validation, and cleaned data handling in one layer.
The settings module controls project-wide configuration such as database connections, installed apps, middleware, and environment-specific behavior.
Signals are useful for decoupled side effects, but too many hidden signal handlers can make code harder to reason about.
Migrations version the schema so database changes stay reproducible across environments.
Use async endpoints when the work awaits non-blocking I O; sync endpoints are fine for CPU work or blocking libraries.
Raise HTTPException for expected API errors and register handlers for shared error formatting.
Middleware wraps the ASGI request-response cycle for cross-cutting behaviors such as timing, headers, and logging.