Opened 5 months ago

Closed 5 months 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 Changed 5 months ago by Carlton Gibson

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 Changed 5 months ago by Timothy Schilling

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 Changed 5 months ago by Carlton Gibson

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