---
django: 6.1.3 (f16e25dcb9fe55734ba4ea2b00ac34405b7df74a)
contents: one-request, design-bets, the-desk
---

# 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.98 milliseconds`** even with zero network round-trip latency. Add two method calls to the queryset — `QuerySet.select_related` and `QuerySet.prefetch_related` — and the response drops to **3 SQL queries** and **`19.65 milliseconds`**, 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.

```text figure=naive-vs-eager
same template and identical HTML bytes, naive view vs. eager view on in-memory SQLite (Django 6.1.3, median of 7 warm runs)

SQL queries per request (0 to 650, to scale)
  Naive · 4 stories     1 in view +  17 in Template.render =  18 SQL  (1,219 HTML bytes)
  Eager · 4 stories     1 in view +   2 in Template.render =   3 SQL  (1,219 HTML bytes)
  Naive · 200 stories   1 in view + 641 in Template.render = 642 SQL  (36,394 HTML bytes)
  Eager · 200 stories   1 in view +   2 in Template.render =   3 SQL  (36,394 HTML bytes)

Measured request time (0 to 220 ms, to scale)
  Naive · 4 stories     0.41 ms view +   6.12 ms render =   6.52 ms
  Eager · 4 stories     0.44 ms view +   1.66 ms render =   2.10 ms
  Naive · 200 stories   0.43 ms view + 208.54 ms render = 208.98 ms  (10.6x slower; 99.8% in render)
  Eager · 200 stories   0.52 ms view +  19.12 ms render =  19.65 ms
```

*Two views, identical HTML, measured on Django 6.1.3. In every run the view itself executes only one SQL statement and returns in half a millisecond; the other 641 queries fire inside `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.43 milliseconds`** 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.render`, when a template loop walked an unevaluated `QuerySet` and triggered hundreds of hidden descriptor lookups (`django/template/response.py:SimpleTemplateResponse.render`, `django/template/base.py:Template.render`).

If you timed the view body with a decorator, wrapped it in `django.db.transaction.atomic`, 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 (`django/core/handlers/wsgi.py:WSGIHandler.__call__`). By the time we reach the closing full-stack latency ladder in [Where Django leaks](leaks.md), 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.close` — in [One request from wire to HTML](introduction/one-request.md).
