CARVIEW |
Select Language
HTTP/2 200
server: nginx
content-type: text/html;charset=utf-8
cache-control: must-revalidate
expires: Fri, 01 Jan 1999 00:00:00 GMT
set-cookie: trac_form_token=964dbbcaf7f36b652041cb65; HttpOnly; Path=/; Secure
set-cookie: trac_session=e768da0e1d3ca6c258e64d77; expires=Tue, 21 Oct 2025 08:42:49 GMT; HttpOnly; Path=/; Secure
strict-transport-security: max-age=31536000; includeSubDomains; preload
permissions-policy: interest-cohort=()
x-content-type-options: nosniff
x-frame-options: SAMEORIGIN
x-xss-protection: 1; mode=block
accept-ranges: bytes
via: 1.1 varnish, 1.1 varnish
date: Wed, 23 Jul 2025 08:42:49 GMT
x-served-by: cache-fra-etou8220057-FRA, cache-bom-vanm7210041-BOM
x-cache: MISS, MISS
x-cache-hits: 0, 0
x-timer: S1753260169.031035,VS0,VE338
vary: Accept-Encoding
LinkifyFilter – Django
Back to Top
Django
The web framework for perfectionists with deadlines.
Issues
Note: There is a built in template tag called urlize that does the same thing.
The following filter will parse out links from the text passed to it and generate anchor tags for them. It will also insert spaces into the textual part of the link, so that long links break properly. Also, you may pass an argument to the filter, specifying the class
attribute of the link.
from django import template import re register = template.Library() regex = re.compile(r'(([a-zA-Z]+)://[^ \t\n\r]+)', re.MULTILINE) def linkify(value, arg=''): def _spacify(s, chars=40): if len(s) <= chars: return s for k in range(len(s) / chars): pos = (k + 1) * chars s = s[0:pos] + ' ' + s[pos:] return s def _replace(match): href = match.group(0) cls = arg and (' class="%s"' % arg) or '' return '<a href="%s"%s>%s</a>' % (href, cls, _spacify(href)) return regex.sub(_replace, value) register.filter('linkify', linkify)
Usage:
{{ variable|linkify:"my_link_class" }}
Last modified
16 years ago
Last modified on Apr 12, 2009, 12:59:10 PM
Note:
See TracWiki
for help on using the wiki.
Download in other formats:
Django Links
Learn More
Get Involved
Follow Us
- Hosting by In-kind donors
- Design by Threespot &
© 2005-2025 Django SoftwareFoundation unless otherwise noted. Django is a registered trademark of the Django Software Foundation.