Module html
HTML utilities suitable for global use.
source linkClasses
Functions
▶ def avoid_wrapping(value) Avoid text wrapping in the middle of a phrase by adding non-breaking spaces where there previously were normal spaces.
▶ def conditional_escape(text) Similar to escape(), except that it doesn't operate on pre-escaped strings.
This function relies on the __html__ convention used both by Django's
SafeData class and by third-party libraries like markupsafe.
Reexports
▶ def escape(text) @keep_lazy(str, SafeString) Return the given text with ampersands, quotes and angle brackets encoded for use in HTML.
@keep_lazy(str, SafeString)
def escape(text)
Always escape input, even if it's already escaped and marked as such.
This may result in double-escaping. If this is a concern, use
conditional_escape() instead.
Reexports
▶ def escapejs(value) @keep_lazy(str, SafeString) Hex encode characters for use in JavaScript strings.
@keep_lazy(str, SafeString)
def escapejs(value)
Reexports
▶ def format_html(format_string, *args, **kwargs) Similar to str.format, but pass all arguments through conditional_escape(), and call mark_safe() on the result. This function should be used instead of str.format or % interpolation to build up small HTML fragments.
▶ def format_html_join(sep, format_string, args_generator) A wrapper of format_html, for the common case of a group of arguments that need to be formatted using the same format string, and then joined using 'sep'. 'sep' is also passed through conditional_escape.
▶ def html_safe(klass) A decorator that defines the __html__ method. This helps non-Django templates to detect classes whose __str__ methods return SafeString.
▶ def json_script(value, element_id) Escape all the HTML/XML special characters with their unicode escapes, so value is safe to be output anywhere except for inside a tag attribute. Wrap the escaped JSON in a script tag.
▶ def linebreaks(value, autoescape=False) @keep_lazy_text Convert newlines into <p> and <br>s.
@keep_lazy_text
def linebreaks(
value,
autoescape=False,
)
Reexports
▶ def strip_tags(value) @keep_lazy_text Return the given HTML with all tags stripped.
▶ def urlize(text, trim_url_limit=None, nofollow=False, autoescape=False) @keep_lazy_text Convert any URLs in text into clickable links.
@keep_lazy_text
def urlize(
text,
trim_url_limit=None,
nofollow=False,
autoescape=False,
)
Works on http://, https://, www. links, and also on links ending in one of
the original seven gTLDs (.com, .edu, .gov, .int, .mil, .net, and .org).
Links can have trailing punctuation (periods, commas, close-parens) and
leading punctuation (opening parens) and it'll still do the right thing.
If trim_url_limit is not None, truncate the URLs in the link text longer
than this limit to trim_url_limit - 1 characters and append an ellipsis.
If nofollow is True, give the links a rel="nofollow" attribute.
If autoescape is True, autoescape the link text and URLs.
Reexports