Opened 3 years ago

Closed 3 years ago

#33692 closed New feature (wontfix)

urlize to accept target attribute as parameter

Reported by: Timothy Schilling Owned by: nobody
Component: Template system Version:
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The anchor tag in HTML supports a target attribute (https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#attr-target) that has a set number of options. It is helpful to be able to control the user's nagivation by specifying whether a link on a page opens in the current view or in a new tab/window.

I propose adding a new parameter to the internal API django.utils.html.urlize and the public template filters urlize and urlizetrunc.

Change History (3)

comment:1 by Carlton Gibson, 3 years ago

Stack Overflow has a suggestion here:

# <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 }}

https://stackoverflow.com/questions/2295725/extending-urlize-in-django

I don't know if that's good enough finally, or whether it's worth adding the API here, but it would serve as a workaround.

comment:2 by Timothy Schilling, 3 years ago

Ah, that will do for me. I probably should have come up with that myself.

I'd be happy to write up the PR for this if there's consensus on adding it. Otherwise this can be closed as the workaround is sufficient.

comment:3 by Carlton Gibson, 3 years ago

Resolution: wontfix
Status: newclosed

Thanks Tim! In that case, let's say wontfix as no-code beats code 9 times out of 10 🙂

Note: See TracTickets for help on using tickets.
Back to Top