37

the urlize function from django.utils.html converts urls to clickable links. My problem is that I want to append a target="_blank" into the "< href..>", so that I open this link in a new tab. Is there any way that I can extend the urlize function to receive an extra argument? or should I make a custom filter using regexes to do this stuff? Is this efficient?

4 Answers 4

67

You can add a custom filter, as described here:

I used this one:

# <app name>/templatetags/url_target_blank.py

from django import template
register = template.Library()

def url_target_blank(text):
    return text.replace('<a ', '<a target="_blank" ')

url_target_blank = register.filter(url_target_blank, is_safe = True)

Example of usage:

{% load url_target_blank %}
...
{{ post_body|urlize|url_target_blank }}

Works fine for me :)

7
  • 10
    I had to add: url_target_blank.is_safe = True to prevent django escaping the link. Jul 8, 2010 at 17:40
  • 2
    To allow broader use of url_target_blank and to make it slightly tighter: I would use a regular expresion in the return line: def url_target_blank(value): return re.sub("<a([^>]+)(?<!target=)>",'<a target="_blank"\\1>',value)
    – Boaz
    Dec 14, 2010 at 10:16
  • 3
    This is a beautiful answer, this should have been the accepted answer
    – Greg Flynn
    Jul 15, 2011 at 17:56
  • I would also use a regexp as replacing every "<a " is a bit too generic for my taste. You should also set url_target_blank.is_safe = True.
    – bjunix
    May 10, 2012 at 13:06
  • @GrahamKing could you edit the answer to show where this needs to be added? Oct 11, 2013 at 7:03
16

Shortest version, that I use in my projects. Create a new filter, that extends the default filter of Django:

from django import template
from django.template.defaultfilters import stringfilter
from django.utils.safestring import mark_safe
from django.utils.html import urlize as urlize_impl

register = template.Library()

@register.filter(is_safe=True, needs_autoescape=True)
@stringfilter
def urlize_target_blank(value, limit, autoescape=None):
    return mark_safe(urlize_impl(value, trim_url_limit=int(limit), nofollow=True, autoescape=autoescape).replace('<a', '<a target="_blank"'))
3
  • You also need from django.utils.safestring import mark_safe
    – Nick
    Aug 26, 2014 at 21:13
  • oh so you can indeed extend the builtin templatetag?
    – Akin Hwan
    Nov 24, 2020 at 16:20
  • urlize has trim_url_limit=None. So I added None as the default: urlize_target_blank(value, limit=None, autoescape=None)
    – jonazu
    Mar 25, 2021 at 12:39
6

There is no capability in the built-in urlize() to do this. Due to Django's license you can copy the code of django.utils.html.urlize and django.template.defaultfilters.urlize into your project or into a separate app and use the new definitions instead.

-3

You shouldn't add target="_blank" to your links, it's deprecated. Users should decide themselves if where they want to open a link.

You can still open the link with unobtrusive JavaScript like so (using jQuery):

$('.container a').click(function(e){e.preventDefault();window.open(this.href);});

That said, you could write your own filter, but you'd have to copy a lot of code from django.utils.html.urlize, not really DRY.

5
  • 2
    Not deprecated anymore in HTML5. Nov 14, 2011 at 13:04
  • "Not deprecated anymore in HTML5" That's true. W3Schools says HTML5 Tag A and another article says XHTML deprecated A target tag Dec 18, 2011 at 21:20
  • W3Schools is not a good source. Rather directly go to the spec.
    – stefanw
    Jun 1, 2012 at 9:39
  • 2
    You are right on w3schools. Here's a source that deserves trust: w3.org/TR/html-markup/a.html#a The target attribute on the a element was deprecated in a previous version of HTML, but is no longer deprecated, as it useful in Web applications, particularly in combination with the iframe element.
    – temoto
    Mar 28, 2013 at 14:58
  • 1
    @ stefanw ... I would argue exactly opposite of what you are proposing. There is no reason to choose a JavaScript method when there is a non-JS method that is better... I'd actually say that in a non-JS web app (a web site in HTML, PHP, Python, etc), you shouldn't use JavaScript unless you have to ( progressive enhancement )... May 13, 2015 at 16:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.