[Django] #33692: urlize to accept target attribute as parameter

101 views
Skip to first unread message

Django

unread,
May 10, 2022, 10:34:15 AM5/10/22
to django-...@googlegroups.com
#33692: urlize to accept target attribute as parameter
---------------------------------------------+------------------------
Reporter: Timothy Schilling | Owner: nobody
Type: New feature | Status: new
Component: Template system | Version:
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
---------------------------------------------+------------------------
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`.

--
Ticket URL: <https://code.djangoproject.com/ticket/33692>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
May 11, 2022, 3:41:44 AM5/11/22
to django-...@googlegroups.com
#33692: urlize to accept target attribute as parameter
-----------------------------------+--------------------------------------

Reporter: Timothy Schilling | Owner: nobody
Type: New feature | Status: new
Component: Template system | Version:
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+--------------------------------------

Comment (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.

--
Ticket URL: <https://code.djangoproject.com/ticket/33692#comment:1>

Django

unread,
May 11, 2022, 9:31:29 AM5/11/22
to django-...@googlegroups.com
#33692: urlize to accept target attribute as parameter
-----------------------------------+--------------------------------------
Reporter: Timothy Schilling | Owner: nobody
Type: New feature | Status: new
Component: Template system | Version:
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+--------------------------------------

Comment (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.

--
Ticket URL: <https://code.djangoproject.com/ticket/33692#comment:2>

Django

unread,
May 11, 2022, 10:03:47 AM5/11/22
to django-...@googlegroups.com
#33692: urlize to accept target attribute as parameter
-----------------------------------+--------------------------------------
Reporter: Timothy Schilling | Owner: nobody
Type: New feature | Status: closed
Component: Template system | Version:
Severity: Normal | Resolution: wontfix

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+--------------------------------------
Changes (by Carlton Gibson):

* status: new => closed
* resolution: => wontfix


Comment:

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

--
Ticket URL: <https://code.djangoproject.com/ticket/33692#comment:3>

Reply all
Reply to author
Forward
0 new messages