format_html() builds a small HTML fragment with Python-style braces, fills its placeholders, and returns a SafeString. It is a tidy alternative to assembling pieces manually and marking the result safe. section 3.7
Format an instrument name
from django.utils.html import format_html instrument = format_html("big <i>{}</i>", "bass") # Rendered output includes: big <i>bass</i> The HTML tags remain active in the template output, while the placeholder supplies the instrument value. Treat escaping controls carefully when values originate from users. section 3.7
Listing 3.14 Using format_html() to mark data as safe in the Django REPL
>>> from django.utils.html import format_html
>>> instrument = "bass"
>>> instrument = format_html("big <i>{}</i>", instrument)
>>> data = {"instrument":instrument}
>>> t.render(Context(data))
"I love the sound a big <i>bass</i> makes.\n
A shiny big <i>bass</i> is good.\n\\n\\n
Don't put a big <i>bass</i> too close to your ear.\n\\n"
Inspect the complete Django REPL example, including the import, formatted value, template context, and rendered output. section 3.7
