In the FILTERS section of the utility_tags.py template library in the utils app, let's add the humanize_url filter and register it, as shown in the following code:
# utils/templatetags/utility_tags.py
import re
from django import template
register = template.Library()
"""FILTERS"""
@register.filter
def humanize_url(url, letter_count):
"""
Returns a shortened human-readable URL
"""
letter_count = int(letter_count)
re_start = re.compile(r"^https?://")
re_end = re.compile(r"/$")
url = re_end.sub("", re_start.sub("", url))
if len(url) > letter_count:
url = f"{url[:letter_count - 1]}..."
return url