• ↑↓ pour naviguer
  • pour ouvrir
  • pour sélectionner
  • ⌘ ⌥ ↵ pour ouvrir dans un panneau
  • ←→ pour naviguer
  • esc pour rejeter
⌘ '
raccourcis clavier

Formal Definition

The Django Template Engine is a text-based templating system that separates presentation logic from business logic using a syntax of variables ({{ variable }}), tags ({% tag %}), and filters ({{ value|filter }}), with support for template inheritance, inclusion, and automatic HTML escaping for security.

Explanation

The template engine solves the problem of generating dynamic HTML (or any text format) without embedding business logic in presentation code. Templates contain only display logic — loops, conditionals, variable interpolation — while views prepare context data. The engine compiles templates to an intermediate representation, then renders them with a context dictionary, auto-escaping variables to prevent XSS unless explicitly marked safe.

How It Works

  1. Template loadedget_template('name.html') or render() loads template from TEMPLATES dirs
  2. Template parsed — Lexer tokenizes into TextNode, VariableNode, BlockNode, IfNode, ForNode
  3. Context created — View builds context dict; context processors add global variables (request, user, messages)
  4. Template renderedtemplate.render(context) walks node tree, resolves variables, executes tags
  5. Auto-escaping applied — All {{ variable }} output passed through escape() unless |safe or mark_safe()
  6. Result returned — Rendered string wrapped in HttpResponse

Visual Explanation

template_engine TemplateFile template.html {% extends 'base.html' %} {% block content %}  {{ user.name }} {% endblock %} Lexer Lexer/Parser → Node Tree TemplateFile->Lexer 1. Parse Renderer Renderer Walk nodes, resolve vars Lexer->Renderer 2. Node tree Context Context Dict {user: User, request: ...} ContextProcessors Context Processors + request, user, messages Context->ContextProcessors 3. Merge ContextProcessors->Renderer 4. Full context AutoEscape Auto-Escape {{ var }} → escape(var) Renderer->AutoEscape 5. Variable nodes Output Rendered HTML <!DOCTYPE html>... AutoEscape->Output 6. Safe string

Semantic Network

semantic_template_engine THIS Template Engine PRE1 View (Context Data) THIS--PRE1 built from PRE2 Template Files (.html) THIS--PRE2 built from PRE3 Context Processors THIS--PRE3 built from OUT1 Template Inheritance THIS--OUT1 builds into OUT2 Template Tags/filters THIS--OUT2 builds into OUT3 Static Files Integration THIS--OUT3 builds into OUT4 Form Rendering THIS--OUT4 builds into CON1 Jinja2 (Standalone) THIS--CON1 contrasts with CON2 React/Vue (Client-side) THIS--CON2 contrasts with REL1 CSRF Token Tag THIS--REL1 related REL2 I18n Translation Tags THIS--REL2 related

Key Properties

  • Auto-escaping by default: {{ user_input }} safe from XSS; opt-out with |safe or mark_safe()
  • Template inheritance: {% extends 'base.html' %} + {% block content %} enables layout reuse
  • Custom tags/filters: @register.simple_tag, @register.filter extend template language
  • Loader flexibility: TEMPLATES['loaders'] supports filesystem, app directories, cached loader
  • Debug integration: TEMPLATE_DEBUG shows template source lines in error pages

Connections

Edge Cases & Gotchas

  • Variable lookup order: Dict key → attribute → list index → callable (no args) → empty string
  • Silent failures: Missing variables render as empty string (configurable via string_if_invalid)
  • |safe danger: Marking user-controlled data as safe enables XSS; only use on trusted content
  • Performance: Uncached template loading hits filesystem on every request; use cached.Loader in production