liveBook · Manning

Mark_safe

As explained in Django in Action

mark_safe is a Django utility that marks a string as safe HTML and returns a SafeString.

Django normally escapes HTML in template data, turning tags such as <b> into visible text. mark_safe() deliberately marks a string as trusted HTML, returning a SafeString that Django will not escape. section 3.7

Use it when the string already contains HTML you intentionally want rendered. Treat it like a “this is safe” sticker, not a security force field: do not apply it casually to untrusted user input. section 3.7

Render an intentional line break

from django.utils.safestring import mark_safe instrument = mark_safe("drum<br/>") data = {"instrument": instrument} # A template value such as {{ instrument }} renders drum<br/> # as an actual line break rather than escaped text. section 3.7

Keep these behavior rules in mind:

  • mark_safe(string) takes the string to trust and returns a SafeString. section 3.7
  • Operations on a SafeString produce an ordinary str, so modifying it requires marking the result safe again. section 3.7
  • For dynamically composed HTML, prefer format_html(), which fills placeholders and returns a SafeString. section 3.7
  • When assembling multiple HTML links in a Django admin column, mark the completed string safe after appending the links. section 5.5
Listing 3.13 Using mark_safe() to mark data as safe in the Django REPL
>>> from django.utils.safestring import mark_safe
>>> instrument = mark_safe("drum<br/>")
>>> data = {"instrument":instrument}
>>> t.render(Context(data))
"I love the sound a drum<br/> makes.\n
 A shiny drum<br/> is good.\n\\n\\n
 Don't put a drum<br/> too close to your ear.\n\\n"

Compare ordinary template escaping with a marked-safe value containing a br tag. section 3.7

How Django in Action covers it

How deep it goes
Explains SafeString behavior, rendering effects, string-operation caveats, and safer dynamic composition, but not implementation internals.
Examples
Demonstrates a drum string with a br tag, formatted italic bass text, and linked admin-column output.
What it assumes
Assumes basic Python strings and Django templates while introducing HTML escaping and SafeString behavior.
What you can do after
Apply mark_safe() to intentional HTML and choose format_html() when constructing HTML from placeholders.
How it connects
Connects template auto-escaping, SafeString, the safe filter, the autoescape tag, format_html(), and Django admin columns.

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.