urlizetrunc Filter

URL

Argument: n (required) – number of characters to truncate the link text to.

Documentation

Like urlize, but truncates the link text to n characters.

Converts URLs in a string to clickable links by wrapping them in an <a> tag. It identifies URLs that begin with http://, https://, or www. It also converts syntactically valid email addresses.

Variable

html = '''Check out our Python training at
www.webucator.com/programming-training/python-training.cfm. If you
have any questions, email sales@webucator.com.'''

Template

{{ html|urlizetrunc:25 }}

Result

Check out our Python training at
<a href="http://www.webucator.com/programming-training/python-training.cfm" rel="nofollow">www.webucator.…</a>. If you
have any questions, email <a href="mailto:sales@webucator.com">sales@webucato…</a>.

The generated links will be nofollow links.

Commentary

Like urlize, this would be more useful if it could be applied safely to text already marked up as HTML. Also, the truncated URLs can be just plain ugly.


Did we get something wrong? Is there a use case for the urlizetrunc filter that we should add? Please let us know.

Send Feedback

Official Documentation
This page last updated on Oct. 30, 2022, 1:21 p.m. EST