Django 6.1.3

Part III · Presentation and systems

Chapter 9: Templates and forms

The promiseA template is HTML with blanks; a form validates fields.

The machineryDjango compiles template source once into a tree of node objects backed by a stacked RenderContext with automatic HTML escaping, while forms run a deterministic three-stage cleaning pipeline that binds raw strings to typed Python values.

When Template receives a string of markup, Lexer slices it into text, variable, tag, and comment tokens and Parser folds those tokens into a NodeList of Node objects. Rendering walks that node tree against a Context, pushing and popping dictionaries so block scopes never leak across sibling nodes.

Compiled once, rendered many times

The default cached template loader keeps compiled Template objects in memory for the life of the worker process. Every request skips lexing and parsing entirely and walks the existing node tree, paying only variable resolution and escaping per render.