liveBook · Manning

HTML escaping

As explained in Django in Action

HTML escaping replaces characters with special meaning in HTML with representations that are displayed as text rather than interpreted as markup.

HTML escaping transforms characters with special meaning in HTML into representations displayed as text instead of interpreted as markup. Django automatically escapes template variables when templates load through the configured rendering engine. section 3.7

Test escaping in a Django template

Create a template containing “I love the sound a {{ instrument }} makes.” Rendering it with “tuba > baritone” produces “tuba &gt; baritone.” A value such as “<b>sax</b>” becomes “&lt;b&gt;sax&lt;/b&gt;,” so the tags appear as text. The Django REPL can load the file through Engine.get_default() and get_template().

Escaping is enabled by default, but trusted markup can bypass it with the safe filter or mark_safe(). format_html() creates formatted safe output; use these only when the markup is intentionally safe. section 3.7

How Django in Action covers it

How deep it goes
Demonstrates escaping through template files, Django’s rendering engine, REPL output, and explicit safe-markup overrides.
Examples
Uses instrument names, greater-than signs, bold tags, the safe filter, mark_safe(), and format_html() as concrete tests.
What it assumes
Assumes basic Django template and REPL usage while explaining HTML angle brackets and entities.
What you can do after
Evaluate auto-escaping with Django’s REPL and distinguish text output from intentionally rendered safe markup.
How it connects
Connects HTML escaping with Django templates, auto-escaping, template engines, safe strings, and HTML entities.

Also explained in

Compare how 3 books explain HTML escaping →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Django in Action. The section links point to the parts of the book it draws on.