utils.safestring.mark_safe()

mark_safe(s) [source]

Explicitly mark a string as safe for (HTML) output purposes. The returned object can be used everywhere a string or unicode object is appropriate.

Can be called multiple times on a single string.

For building up fragments of HTML, you should normally be using django.utils.html.format_html() instead.

String marked safe will become unsafe again if modified. For example:

1
2
3
4
5
6
7
8
>>> mystr = '<b>Hello World</b>   '
>>> mystr = mark_safe(mystr)
>>> type(mystr)
<class 'django.utils.safestring.SafeBytes'>
 
>>> mystr = mystr.strip()  # removing whitespace
>>> type(mystr)
<type 'str'>
doc_Django
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.