Chapter 1
Chapter 1: One request, unwrapped
Follow a single HTTP request through Django from WSGI entry to SQL and HTML, and see the framework’s core design bets in action.
Suppose you ask an engineer — or a coding agent — to write a Django page listing the published stories in a newspaper edition, with each story’s desk, reporter, and correction count beside the headline. Ten seconds later it comes back with a twelve-line subclass of ListView, a clean template, a passing test on four sample stories, and a brief report: it works.
On those four stories, the page renders in 6.5 milliseconds on in-memory SQLite, returns 200 OK, and displays the exact HTML requested. Nothing in the view looks wrong. Nothing in the template looks slow.
Hand that exact same view a two-hundred-story Sunday edition and the machinery underneath breaks through the floor. Without changing a single line of code, the request fires 642 SQL queries and takes 208 even with zero network round-trip latency. Add two method calls to the queryset — QuerySet and QuerySet — and the response drops to 3 SQL queries and 19, producing the exact same 36,394 bytes of HTML, byte for byte.
Where the 642 queries actually ran
The sharper surprise is not the tenfold slowdown. It is when the 642 queries executed.
django.template.base.Template.render after the view has already finished.Look at the view-time numbers. On the two-hundred-story Sunday edition, the naive view returned in 0 after running a single SQL lookup for the edition row. Then the view ended. Everything expensive — all 641 remaining SQL statements and 99.8% of the wall-clock time — happened afterwards, inside SimpleTemplateResponse, when a template loop walked an unevaluated QuerySet and triggered hundreds of hidden descriptor lookups.
If you timed the view body with a decorator, wrapped it in django, or read the view code in isolation, you would miss almost every database query the endpoint sent.
What this book opens
Django is built out of promises like that one. A request looks like a function call; a model looks like a Python class; a queryset behaves like a list; a template is HTML with blanks; a migration is a diff of your models; security happens automatically. Every one of those promises is true enough to ship software on, and every one is paid for by hidden machinery — metaclasses, lazy AST compilers, descriptor caches, thread-pool bridges, nested savepoints, historical state graphs — that costs latency, allocates memory, or fails silently when code steps outside its assumptions.
Whether you write Django yourself, debug it in production, or judge code written by an agent that only proved its view works on four rows, you need a working model of the machinery underneath: what runs, when it runs, what it costs, and where each abstraction stops keeping its word.
Every claim in this book is verified against Django 6.1.3 (commit f16e25d) and measured live on a single small newsroom project rather than invented from memory. When a page quotes what Django ran, those lines come verbatim from executing the pinned release; when a mechanism is named, its source pointer sits in the margin beside the prose. By the time we reach the closing full-stack latency ladder in Where Django leaks, every millisecond of that edition page — from URLResolver and middleware through ORM compilation, template rendering, and TestCase rollbacks — will be accounted for.
We begin by running that edition request twice — first the plausible version that quietly fires 642 queries, then the full trace from the WSGI server dictionary to HttpResponseBase — in One request from wire to HTML.